I think it will be useful for you
You should implementation this:
dependencies { implementation "androidx.lifecycle:lifecycle-extensions:2.2.0" }
You should write this:
class App : Application() { override fun onCreate() { super.onCreate() ProcessLifecycleOwner.get().lifecycle.addObserver(lifecycleEventObserver) }
You should write this: In here I implemented only two Lifecycle Event, when you need other Lifecycle Event, you should implement them
var lifecycleEventObserver = LifecycleEventObserver { _, event -> when (event) { Lifecycle.Event.ON_STOP -> { //your code here } Lifecycle.Event.ON_START -> { //your code here } else -> {} } }
class App : Application() { override fun onCreate() { super.onCreate() ProcessLifecycleOwner.get().lifecycle.addObserver(defaultLifecycleObserver) }
var defaultLifecycleObserver = object : DefaultLifecycleObserver { override fun onStart(owner: LifecycleOwner) { super.onStart(owner) //your code here } override fun onStop(owner: LifecycleOwner) { super.onStop(owner) //your code here } }
class App : Application() { override fun onCreate() { super.onCreate() registerActivityLifecycleCallbacks(activityLifecycleCallbacks) }
private val activityLifecycleCallbacks = object : ActivityLifecycleCallbacks { override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) { } override fun onActivityStarted(activity: Activity) { } override fun onActivityResumed(activity: Activity) { } override fun onActivityPaused(activity: Activity) { } override fun onActivityStopped(activity: Activity) { } override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) { } override fun onActivityDestroyed(activity: Activity) { } }
Copyright © 2021 Jogjafile Inc.
1 - LifecycleEventObserver
Firstly
You should implementation this:
Secondly
You should write this:
Thirdly
You should write this:
In here I implemented only two Lifecycle Event, when you need other Lifecycle Event, you should implement them
2 - DefaultLifecycleObserver
Firstly
You should implementation this:
Secondly
You should write this:
Thirdly
You should write this:
In here I implemented only two Lifecycle Event, when you need other Lifecycle Event, you should implement them
3 - ActivityLifecycleCallbacks
Firstly
You should write this:
Secondly
You should write this: