Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why LiveData still notify Activity which is in onPause state?

I have the following code in Activity

@Override
public void onPause() {
    super.onPause();

    if (isFinishing()) {
        final LiveData<StickyNoteConfig> stickyNoteConfigLiveData = StickyNoteConfigRepository.INSTANCE.getStickyNoteConfig(mAppWidgetId);
        stickyNoteConfigLiveData.removeObservers(this);
        stickyNoteConfigLiveData.observe(this, stickyNoteConfig -> {
            // Weird, I still can receive call back.
            // I thought "this" is no longer active?
        });
    }
}

I feel puzzled that, Observer is still being triggered, although this activity is already in onPause state? According to https://developer.android.com/reference/android/arch/lifecycle/Lifecycle.State#STARTED

Started state for a LifecycleOwner. For an Activity, this state is reached in two cases:

after onStart call;
right before onPause call.

May I know why it is so?

like image 559
Cheok Yan Cheng Avatar asked Oct 02 '18 22:10

Cheok Yan Cheng


2 Answers

https://medium.com/@hanyuliu/livedata-may-lose-data-2fffdac57dc9 has logs to explain this issue. And just like @Vairavan said,in pause state activity can be partially visible.

public enum State {

DESTROYED,

INITIALIZED,

CREATED,

STARTED,

RESUMED;

public boolean isAtLeast(@NonNull State state) {
    return compareTo(state) >= 0;
}

}

so, observer's isAlive judge according to isAtLeast(STARTED). when OnPause is called, activity does not become DESTROYED.

like image 66
Jenus Dong Avatar answered Oct 19 '22 09:10

Jenus Dong


According to LiveData reference,

  1. LiveData is a data holder class that can be observed within a given lifecycle. This means that an Observer can be added in a pair with a LifecycleOwner, and this observer will be notified about modifications of the wrapped data only if the paired LifecycleOwner is in active state. LifecycleOwner is considered as active, if its state is STARTED or RESUMED.

  2. An observer added with a Lifecycle will be automatically removed if the corresponding Lifecycle moves to DESTROYED state.

Now, according to your situation here, LiveData receives updates for your observer (your activity) in onPause() method, because your observer is not already in DESTROYED state.

So LiveData is still active for receive updates according to these methods:

onActive() :

Called when the number of active observers change to 1 from 0. This callback can be used to know that this LiveData is being used thus should be kept up to date.

&

onInactive() :

Called when the number of active observers change from 1 to 0. This does not mean that there are no observers left, there may still be observers but their lifecycle states aren't STARTED or RESUMED (like an Activity in the back stack).

You can check if there are observers via hasObservers().


So when does observer (your activity) gets DESTROYED state?

For default implementation of LifeCycleOwner indicates that activity gets it's DESTROYED state once onDestroy() method is executed & after onPause() it follows reverse order of LifeCycle state RESUMED -> STARTED -> CREATED -> DESTROYED.

Check this lifecycle graph.

Hope it helps !

like image 39
Jeel Vankhede Avatar answered Oct 19 '22 09:10

Jeel Vankhede