How to open two different activity on recyclerView - Kotlin - kotlin

I am using a recyclerView to show a list of card-movies in the mainActivity and I have implemented an onclickListener that open the details page.
If I wanted one of the cards in the list to open a different activity, how can I set the click or, if necessary, the other elements I use?

on your onClick() method call the position of the card as follows
#Override
public void onClick(View v) {
final Intent intent;
switch (getAdapterPostion()){
case 0:
intent = new Intent(context, FirstActivity.class);
break;
case 1:
intent = new Intent(context, SecondActivity.class);
break;
...
default:
intent = new Intent(context, DefaultActivity.class);
break;
}
context.startActivity(intent);
}

Related

Issues related to moving to MainActivity by pressing a button

If the user presses the positive button, they want to move it to MainActivity
fun setPositiveButtonClick(callback: (() -> Unit)?, view: View? = null) {
val intent = Intent(this#ErrorDialog, MainActivity::class.java)
startActivity(intent)
var view = view
if (view == null) view = this.view
this.positiveCallback = callback
view?.let { v ->
v.yes.setOnClickListener {
callback?.let { c ->
c()
}
dismiss()
}
}
}
Here is the error
I'm assuming this is in some sort of Dialog class, so I think you simply need to get the context of it by just replacing
val intent = Intent(this#ErrorDialog, MainActivity::class.java)
to
val intent = Intent(this.context, MainActivity::class.java)

Unable to call SetOnClickListener

Unable to run OnSetClickLestiner
as I can see you cant find the button with the id. You have to options
1st option: go to ur code and use the line
val myButton: Button = findViewById(R.id.button)
myButton.setOnClickListener{
...
}
2nd option: go to gradle.app file and add into the plugins the line:
id 'kotlin-android-extensions'
and then keep the same code as you have
Follow this format when calling setOnClickListener
Always initialize your Button variables before calling them.
public class MyActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_layout_id);
Button button = findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code here executes on main thread after user presses button
}
});
}
}

Avoid fragment recreation when opening from notification navigation component

I want when I click on a notification to open a fragment and not recreate it. I am using navigation component and using NavDeepLinkBuilder
val pendingIntent = NavDeepLinkBuilder(this)
.setComponentName(MainActivity::class.java)
.setGraph(R.navigation.workouts_graph)
.setDestination(R.id.workoutFragment)
.createPendingIntent()
My case is I have a fragment and when you exit the app, there is a notification which when you click on it, it should return you to that same fragment. Problem is every time i click on it it's creating this fragment again, I don't want to be recreated.
I had the same issue. Looks like there is not an option to use the NavDeepLinkBuilder without clearing the stack according to the documentation
I'm not sure the exact nature of your action, but I'll make two assumptions:
You pass the destination id to your MainActivity to navigate.
Your MainActivity is using ViewBinding and has a NavHostFragment
You will have to create the pending intent like:
val intent = Intent(this, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_SINGLE_TOP
putExtra("destination", R.id.workoutFragment)
}
val pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
And in your MainActivity, you can handle both cases (app was already open, app was not already open)
override fun onStart() {
super.onStart()
// called when application was not open
intent?.let { processIntent(it) }
}
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
// called when application was open
intent?.let { processIntent(it) }
}
private fun processIntent(intent: Intent) {
intent.extras?.getInt("destination")?.let {
intent.removeExtra("destination")
binding.navHostFragment.findNavController().navigate(it)
}
}

submitList() in recyclerview with diffutil causes recyclerview to blink

I have a recyclerview that's been updated from remote source ,so it is working pretty well , the only annoying thing is that when the screen is pulled for a refresh and a get request is triggered the recyclerview is updated even though nothing has changed in the adapter causing the screen to blinks and that's annoying.
i tried messing with the diffutils but no result achieved
in my main fragment:
viewModel.fetchTopHeadline()
followed by an observer
topHeadline.observe(this#FeedFragment, Observer {
if(it == null) return#Observer
topHeadlineAdapter.submitList(it)
})
my Adapter:
class TopHeadlineAdapter:ListAdapter
and finally my diffutilCallback class :
class TopHeadlineDiffCallback :DiffUtil.ItemCallback<Article>(){
override fun areItemsTheSame(oldItem: Article, newItem: Article): Boolean {
return oldItem.id == newItem.id
}
override fun areContentsTheSame(oldItem: Article, newItem: Article): Boolean {
return oldItem == newItem
}
}

Exepting member declaration [duplicate]

I'm trying to finish an activity from another (android) with kotlin. I know the wat to do it with java is with the following code (https://stackoverflow.com/a/10379275/7280257)
at the first activity:
BroadcastReceiver broadcast_reciever = new BroadcastReceiver() {
#Override
public void onReceive(Context arg0, Intent intent) {
String action = intent.getAction();
if (action.equals("finish_activity")) {
finish();
// DO WHATEVER YOU WANT.
}
}
};
registerReceiver(broadcast_reciever, new IntentFilter("finish_activity"));
On the other activity:
Intent intent = new Intent("finish_activity");
sendBroadcast(intent);
For some reason converting the java activity to kotlin doesn't give a valid output, if someone could give me the correct syntax to do it properly with kotlin I will appreciate it
kotlin output (first activity) [OK]:
val broadcast_reciever = object : BroadcastReceiver() {
override fun onReceive(arg0: Context, intent: Intent) {
val action = intent.action
if (action == "finish_activity") {
finish()
// DO WHATEVER YOU WANT.
}
}
}
registerReceiver(broadcast_reciever, IntentFilter("finish_activity"))
kotlin output (2nd activity) [OK]
val intent = Intent("finish_activity")
sendBroadcast(intent)
ERROR: http://i.imgur.com/qaQ2YHv.png
FIX: THE CODE SHOWN IS RIGHT, YOU JUST NEED TO PLACE IT INSIDE THE onCreate FUNCTION
Simple code to finish a particular activity from another:
class SplashActivity : AppCompatActivity(), NavigationListner {
class MyClass{
companion object{
var activity: Activity? = null
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
MyClass.activity = this#SplashActivity
}
override fun navigateFromScreen() {
val intent = Intent(this,LoginActivity::class.java)
startActivity(intent)
}
}
Now call SplashActivity.MyClass.activity?.finish() from another activity to finish above activity.
The error Expecting member declaration is there because you wrote a statement (the function call) inside a class. In that scope, declarations (functions, inner classes) are expected.
You have to place your statements inside functions (and then call those from somewhere) in order for them to be executed.