Skip to content

Instantly share code, notes, and snippets.

@mochadwi

mochadwi/SafeActivity.kt

Last active Feb 16, 2020
Embed
What would you like to do?
Wrapper to handle nullable activity and returns non-null activity
fun Activity?.safeActivity(action: Activity.() -> Unit) {
val isActivityStillRuns = this != null && (!this?.isDestroyed() || !this?.isFinishing())
if (isActivityStillRuns) {
action.invoke()
}
}
// usage:
fun initSomething() = this@YourActivity.safeActivity {
val adapter = YourAdapter(this, listOf())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment