Skip to content

Instantly share code, notes, and snippets.

@fo2rist
Created July 9, 2019 18:50
Show Gist options
  • Save fo2rist/91028a333dce2ae63cd7e05f2e35fc8b to your computer and use it in GitHub Desktop.
Save fo2rist/91028a333dce2ae63cd7e05f2e35fc8b to your computer and use it in GitHub Desktop.
ActivityThatMayCrash.kt
class SomeStrictActivity : Activity() {
companion object {
@JvmStatic
fun createIntent(context: Context, importantParameter: Parameter): Intent {
val intent = Intent(context, SomeStrictActivity::class.java)
intent.putExtra(EXTRA_PARAMETER, importantParameter)
return intent
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val parameter = intent.getSerializableExtra(EXTRA_PARAMETER)
// if launched properly this should never happen, so it's OK to crash when getSerializableExtra gives null
?: throw IllegalArgumentException("Required parameter $EXTRA_PARAMETER is missing in the intent.")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment