Created
October 11, 2019 20:08
-
-
Save reline/5f00f0ce0704b63c267c90acdea8bf0f to your computer and use it in GitHub Desktop.
Android type safe intents
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
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