Skip to content

Instantly share code, notes, and snippets.

@reline
Created October 11, 2019 20:08
Show Gist options
  • Save reline/5f00f0ce0704b63c267c90acdea8bf0f to your computer and use it in GitHub Desktop.
Save reline/5f00f0ce0704b63c267c90acdea8bf0f to your computer and use it in GitHub Desktop.
Android type safe intents
private const val SHOW_GREETING_TOAST = "SHOW_GREETING_TOAST"
// alternatively, make this public for other activities to use
private var Intent.showGreetingToast: Boolean
set(value) { putExtra(SHOW_GREETING_TOAST, value) }
get() = getBooleanExtra(SHOW_GREETING_TOAST, false)
class MyActivity : Activity() {
companion object {
fun newIntent(context: Context, showGreetingToast: Boolean): Intent {
val intent = Intent(context, MyActivity::class.java)
intent.showGreetingToast = showGreetingToast
return intent
}
}
private val showGreetingToast by lazy { intent.showGreetingToast }
override fun onCreate(savedInstanceState: Bundle?) {
if (showGreetingToast) toast(android.R.string.ok)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment