Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it mandatory to remove yourself as an observer from Android Lifecycle?

I am building an Android Java class which implements the LifecycleObserver interface.

This is the constructor:

public MyObserver(AppCompatActivity activity) {
    this.mActivity = new WeakReference<AppCompatActivity>(activity);
    activity.getLifecycle().addObserver(this);
}

Is it necessary to ever call removeObserver, using something like:

@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void destroyListener() {
    if (this.mActivity.get() != null) {
        this.mActivity.get().getLifecycle().removeObserver(this);
    }
}

Or, can I observe forever?

like image 716
esilver Avatar asked Dec 05 '17 15:12

esilver


People also ask

Do I need to call removeObserver?

addObserver in application, you don't have to call removeObserver , when the application is destroyed, the process would be killed also.

What is lifecycle observer in Android?

LifeCycleObserver is part of Google released Android Jetpack LifeCycle Architecture components, and it is an interface that allows you to observe a LifeCycle-aware observable component, typically a LifeCycleOwner (Activity/Fragment), in order to interact with the LifeCycle events and states associated to this component ...

What is life cycle view owner?

LifecycleOwner is a single method interface that denotes that the class has a Lifecycle . It has one method, getLifecycle() , which must be implemented by the class. If you're trying to manage the lifecycle of a whole application process instead, see ProcessLifecycleOwner .


3 Answers

TL;DR: Nope.

According to this link here, where a user asked your question on the android-lifecycles Github repo. The answer of a Google developer to this questions was:

Yes, that's the whole point of the new lifecycle-aware components, no need to unsubscribe/remove observers.

like image 137
TareK Khoury Avatar answered Oct 16 '22 17:10

TareK Khoury


TL;DR: You're better off explicitly removing the observer when you are done with it, or use something that handles this automatically, such as LiveData.

Lifecycle is an abstract class. So, technically, you have no idea what the implementation is and what the rules of the game are.

The one concrete Lifecycle is LifecycleRegistry. It has strong references to the observers. So now you are counting on the LifecycleRegistry being garbage-collected in a timely fashion, such as when the activity is destroyed. For FragmentActivity, that appears to be the case. So in practice, for the now-current version of all this stuff, you could get away without unregistering the observer and suffer few ill effects, if any.

But that's not part of the Lifecycle contract. Arguably, any decent implementation of Lifecycle (or things that use LifecycleRegistry) should cleanly handle the case where you fail to unregister... but I wouldn't risk it.

like image 29
CommonsWare Avatar answered Oct 16 '22 15:10

CommonsWare


I'm using LifecycleRegistry in a singleton object that manages audio sounds. After adding LeakCanary it detected a memory leak because of this issue.

However, calling removeObserver, the memory leak never appeared again.

So it may be true that when using LiveData + LifecycleRegistry you don't need to unregister. But if it's a custom component using LifecycleRegistry, my experience shows that calling removeObserver is a must to avoid memory leaks.

like image 1
Vito Valov Avatar answered Oct 16 '22 17:10

Vito Valov