简体   繁体   中英

How to launch an Android Activity when multiple pages of the App have Same Activity name in Kotlin?

I have a screen consisting list of Results. Clicking on each of the result takes me to different item but with the same Activity Name(TestResultActivityDetails). How can I launch each Result as different Activity Launcher? Is there any option to use index on list items to launch Activity?

val intent = Intent(TestResultActivityDetails::class.java)
startActivity(intent)

Here, TestResultActivityDetails::class.java is the activity name which is shared with all the list of Results.

To pass data to an Activity you need to use Intent extras. Your posted code would become as this:

val intent = Intent(TestResultActivityDetails::class.java).apply {
    /* You pass data as extras, with a key and the actual value.
     *         KEY            VALUE */
    putExtras("clickedIndex", index)
}

startActivity(intent)

Then, in TestResultActivityDetails you would do:

override fun onCreate(saveInstance: Bundle?) {
    ...

    /* You get your data back from the Intent extras, using previous key.
     *                                 KEY               FALLBACK if not found */
    val index = intent.extras?.getInt("clickedIndex") ?: -1
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM