Skip to content

Instantly share code, notes, and snippets.

@dinorahtovar
Last active November 15, 2018 16:20
Show Gist options
  • Save dinorahtovar/963837024ff6e7583ff5afe1602a1544 to your computer and use it in GitHub Desktop.
Save dinorahtovar/963837024ff6e7583ff5afe1602a1544 to your computer and use it in GitHub Desktop.
LifecycleObserver to check OnPause and OnResume
/**
* Created by Dinorah Tovar on 2/22/18.
* Application Owner using lifecycleObserver to know the application of
* the application.
*/
class ApplicationOwner : LifecycleObserver {
/**
* Companion Object
*/
companion object {
var observer: Boolean = false
var observerDestroy: Boolean = false
}
/**
* Application is starting again
*/
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onStartApplication() {
if (observer) {
val intent = Intent(context, SplashActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.applicationContext.startActivity(intent)
}
}
/**
* Application was stopped from user perspective
*/
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun onStopApplication() {
if (observerDestroy) {
observer = false
observerDestroy = false
} else {
observer = true
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment