简体   繁体   中英

How to finish an activity from RecyclerView adapter class using Kotlin

I am new to Kotlin programming language and I want to finish my RecyclerView activity from adapter class. How do I do it? It used to be quite easy in java but I just switched to Kotlin and I am facing this problem.

you can use finish() method ,

(context as YourActivity).finish()

this method will help you to finish the respective activity.

If you use ((Activity)context).finish(); in java than in kotlin you can use (context as Activity).finish()

Try it.

Create this method in the activity:

fun finishMe() { finish() }

In your adapter declare this:

private val activity : MainActivity = context as MainActivity

replace MainActivity with the activity's name and context is the activity's Context passed to the adapter as a parameter (I believe it exists).
Now you can finish the activity anywhere in the adapter by:

activity.finishMe()

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