Skip to content

Instantly share code, notes, and snippets.

@passsy
Last active September 17, 2017 10:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save passsy/60b5f65446c95590f5610f5195799620 to your computer and use it in GitHub Desktop.
Save passsy/60b5f65446c95590f5610f5195799620 to your computer and use it in GitHub Desktop.
How I create Intents and start Activities in Kotlin
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" }
}
}
// 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