How to generate a new activity on a click

Zach Garber :

I fairly new to android studio and I can't seem to find anywhere to start with my problem. I have a Recycler View that works fine and I have the click on individual cards functionality working fine. However, I have a problem; is it possible to generate a completely new activity for each item in a recycler view? The size of the recycler view will always be changing. The activity that clicking on a card will take you to will be the same template just with different information. So, As one card is deleted, so to will the activity that it is linked to, and as one is created via a button, an empty template will be linked to it waiting for the user to input information. So, there may be dozens of these activities all the same just with different information. Any information regarding this would be appreciated.

edit: I do know how to use intents, my problem isn't navigating between activities, it is generating a new activity, once a new card in the recycler view is added, that is a template of a predesigned activity linked to the newly created card view. The generated activites would have to be independent of each other with different user entered info added into edit texts. I just do not know how to automatically create a new version of an activity for each card created.

Olehcoding :

Create an interface in your adapter

public interface onItemClickListener{
    void onAcitivityCreated();
}

create instance on your interface like

private onItemClickListener onitemClickListener; 

then pass it as a parameter in your constructor in adapter

in your activity when creating adapter object you will have to pass (this) and also implement interface in your activity like: MyActivity implements onItemClickListener and implement all methods and here you can write all your realization of the method,so you wont have to do this in your adapter.

At last you can create static method in your activities and then you can easily use it to open new activity.

public static void open(@NonNull Context context) {
    context.startActivity(new Intent(context, YourActivity.class));
}

Main stuff,when you have your item, cardview etc in adapter. You are implementing onclick to open new activity. When implementing it you will have to pass your interface and just pass whatever parameter you need there.

Might be complicated,but its the best solution here.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=151379&siteId=1