Skip to content

Instantly share code, notes, and snippets.

@shibbirweb
Created March 12, 2021 10:47
Show Gist options
  • Select an option

  • Save shibbirweb/d6c08eafa7f75a7702e7029424c79fb0 to your computer and use it in GitHub Desktop.

Select an option

Save shibbirweb/d6c08eafa7f75a7702e7029424c79fb0 to your computer and use it in GitHub Desktop.
Android: Context to activity
package me.shibbir.utils.helpers
import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
object ActivityHelper {
/**
* Get activity
*/
fun contextToActivity(context: Context): Activity? {
if (context is ContextWrapper) {
if (context is Activity) {
return context
} else {
contextToActivity(context.baseContext)
}
}
return null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment