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?
addObserver in application, you don't have to call removeObserver , when the application is destroyed, the process would be killed also.
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 ...
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 .
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With