Skip to content

Instantly share code, notes, and snippets.

@hichamboushaba
Last active November 16, 2021 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hichamboushaba/0234bc6f089df5fdbd3228dc9c530ebd to your computer and use it in GitHub Desktop.
Save hichamboushaba/0234bc6f089df5fdbd3228dc9c530ebd to your computer and use it in GitHub Desktop.
class ActivityProvider : Application.ActivityLifecycleCallbacks {
private var _currentActivity = WeakReference<ComponentActivity>(null)
val currentActivity
get() = _currentActivity.get()
fun init(application: Application) = application.registerActivityLifecycleCallbacks(this)
override fun onActivityCreated(activity: Activity, bundle: Bundle?) {}
override fun onActivityStarted(activity: Activity) {
(activity as? ComponentActivity)?.let {
_currentActivity = WeakReference(it)
}
}
override fun onActivityResumed(activity: Activity) {
(activity as? ComponentActivity)?.let {
_currentActivity = WeakReference(it)
}
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment