8

Answer is on bottom

I think it will be useful for you

ONVETI
  • 352
  • 3
  • 11

1 Answers1

12

- LifecycleEventObserver

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

- 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
        }
    }
ONVETI
  • 352
  • 3
  • 11