Lifecycle @OnLifecycleEvent() is deprecated, and you should use LifecycleEventObserver or DefaultLifecycleObserver [duplicate]
Asked Answered
A

1

9

Answer is on bottom

I think it will be useful for you

Amputee answered 17/5, 2022 at 9:34 Comment(0)
A
15

1 - LifecycleEventObserver

Firstly

You should implementation this: more info

Old dependency have been deprecated:

implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"

New dependency, You should use this

dependencies {    
    implementation"androidx.lifecycle:lifecycleprocess:2.8.3"
}

Secondly

You should write this:

class App : Application() {
    override fun onCreate() {
        super.onCreate()

        ProcessLifecycleOwner.get().lifecycle.addObserver(lifecycleEventObserver)

    }

Thirdly

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 -> {}
        }
    }

2 - DefaultLifecycleObserver

Firstly

You should implementation this:

dependencies {
    implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
}

Secondly

You should write this:

class App : Application() {
    override fun onCreate() {
        super.onCreate()

        ProcessLifecycleOwner.get().lifecycle.addObserver(defaultLifecycleObserver)

    }

Thirdly

You should write this:
In here I implemented only two Lifecycle Event, when you need other Lifecycle Event, you should implement them

    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
        }
    }

3 - ActivityLifecycleCallbacks

Firstly

You should write this:

class App : Application() {
    override fun onCreate() {
        super.onCreate()

        registerActivityLifecycleCallbacks(activityLifecycleCallbacks)

    }

Secondly

You should write this:

    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) {

        }

    }
Amputee answered 17/5, 2022 at 9:34 Comment(1)
androidx.lifecycle:lifecycle-extensions is now changed to androidx.lifecycle.processBugbear

© 2022 - 2024 — McMap. All rights reserved.