Skip to content

Instantly share code, notes, and snippets.

@mochadwi
Last active February 16, 2020 15:46
Show Gist options
  • Save mochadwi/9091d3d5e1789db5669adc51df2533bd to your computer and use it in GitHub Desktop.
Save mochadwi/9091d3d5e1789db5669adc51df2533bd to your computer and use it in GitHub Desktop.
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