Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare startActivityforResult() in one line in Kotlin?

Tags:

kotlin

I want to call startActivityforResult in a one line statement in Kotlin. Something like this:

startActivityForResult(Intent(this@MainActivity2,MainActivity::class.java),1))
like image 729
Ujjval Avatar asked Dec 03 '22 11:12

Ujjval


1 Answers

Option 1: Use Anko library:

startActivityForResult<MainActivity>(1)

Option 2: You can create your own extension Function on Activity or AppCompactActivity:

inline fun <reified T: Activity> Activity.myStartActivityForResult(requestCode: Int) {
    val intent = Intent(this, T::class.java)
    startActivityForResult(intent, requestCode)
}

myStartActivityForResult<MainActivity>(1)
like image 153
JTeam Avatar answered Dec 08 '22 03:12

JTeam