-
-
Save passsy/60b5f65446c95590f5610f5195799620 to your computer and use it in GitHub Desktop.
How I create Intents and start Activities in Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun Context.UserDetailIntent(user: User): Intent { | |
return Intent(this, UserDetailActivity::class.java).apply { | |
putExtra(INTENT_USER_ID, user.id) | |
} | |
} | |
private const val INTENT_USER_ID = "user_id" | |
class UserDetailActivity : Activity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
val userId = intent.getStringExtra(INTENT_USER_ID) | |
requireNotNull(userId) { "no user_id provided in Intent extras" } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Usage in other Activities | |
class OtherActivity: Activity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
//... | |
userList.clickListener { user -> | |
val options = buildYourOwnOptionsDependingOnYourNeed() // only when required | |
startActivity(UserDetailIntent(user), options) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment