Home > front end >  How can i create activties when pressing on Recycleview Item
How can i create activties when pressing on Recycleview Item

Time:09-18

im new to programming apps for android in android studio with kotlin.

im trying to make my own application and i made a concept of how it should work but i cant really help myself, and it is hard to google it because i dont know what to google for.

What i have done already is to implement recylceview and did a class for adapt items in recylceview, setup room database, and all of this works...

But now i want to make that when user press on Item-Button in Recycleview it should open a new activity with a blank layout(where you can add items in recylceview again). But now when the user creates another item in home activity and presses the button it shouldnt open the activity of item number 1, it should open a new activity with new blank layout.

This is my concept how it should work like:

enter image description here

CodePudding user response:

Assuming you have the second activity that you want to launch already created, you can do the following inside your recycler view items onClick method:

val intent = Intent(this, MySecondActivity::class.java)
startActivity(intent)

You want to keep the second activity generic here. So let's say your recycler view displays a list of recipes, and when you click on a recipe, a detailed screen is shown with more info about that specific recipe. In your MySecondActivity activity, you would have text for example that gives the user more detail about the recipe they just clicked on. You would pass this text into the activity via the intent.

intent.putExtra("key", detailedText)

And in the second activity you would set the text views text to the passed in text from the intent.

val passedInInfo: Bundle = getIntent().getExtras()
val detailedText: String = passedInInfo.getString("key")

The second activity will also need to be added to your manifest file. Something like this:

<activity android:name=".MySecondActivity"
          android:parentActivityName=".MainActivity">
    <!-- The meta-data tag is required if you support API level 15 and lower -->
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value=".MainActivity" />
</activity>

I'd recommend reading through this link! https://developer.android.com/training/basics/firstapp/starting-activity

  • Related