Skip to content

Instantly share code, notes, and snippets.

@rahulsainani
Created September 12, 2021 11:36
Show Gist options
  • Select an option

  • Save rahulsainani/aabcac3b39f663bd205ca07b30468a09 to your computer and use it in GitHub Desktop.

Select an option

Save rahulsainani/aabcac3b39f663bd205ca07b30468a09 to your computer and use it in GitHub Desktop.
/**
* Find the closest Activity in a given Context.
*/
internal fun Context.findActivity(): Activity {
var context = this
while (context is ContextWrapper) {
if (context is Activity) return context
context = context.baseContext
}
throw IllegalStateException("Permissions should be called in the context of an Activity")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment