Muhammad Faizan
Muhammad Faizan

Reputation: 2088

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.

Upvotes: 0

Views: 2673

Answers (3)

Sachin Rajput
Sachin Rajput

Reputation: 4344

you can use finish() method ,

(context as YourActivity).finish()

this method will help you to finish the respective activity.

Upvotes: 4

forpas
forpas

Reputation: 164089

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()

Upvotes: 4

Rozina
Rozina

Reputation: 409

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

Try it.

Upvotes: 2

Related Questions