I am creating an Android Java class that implements an interface . LifecycleObserver
LifecycleObserver
This is the constructor:
public MyObserver(AppCompatActivity activity) { this.mActivity = new WeakReference<AppCompatActivity>(activity); activity.getLifecycle().addObserver(this); }
Do I ever need to call using something like: removeObserver
removeObserver
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) public void destroyListener() { if (this.mActivity.get() != null) { this.mActivity.get().getLifecycle().removeObserver(this); } }
Or can I watch forever?
TL DR: Nope.
This link is here where the user asked his question about the android-lifecyclesGithub repo. The answer of the Google developer to these questions was:
android-lifecycles
Yes, that the whole point of the new components, taking into account the life cycle, there is no need to unsubscribe / delete observers.
TL; DR: , , , , LiveData.
LiveData
Lifecycle abstract. , , .
Lifecycle
abstract
Lifecycle LifecycleRegistry. . , , LifecycleRegistry , , . FragmentActivity . , , , , , .
LifecycleRegistry
FragmentActivity
Lifecycle. , Lifecycle ( , LifecycleRegistry) , ... .
LifecycleRegistry , . LeakCanary - .
, removeObserver, .
Thus, it may be true that when using LiveData + LifecycleRegistry you do not need to unregister. But if this is a custom component using LifecycleRegistry, my experience shows that the call to removeObserver is mandatory to avoid memory leaks.
Source: https://habr.com/ru/post/1690274/More articles:How to get the equation of density distribution in SAS? - sasWhy does the "debugger" prevent the event from firing (keydown → onInput) - javascriptThe PrtScn button in IE does not behave like other browsers using java script - javascriptCSS Transparent Gold Background - htmlНевозможно загрузить неопытные модели в automl() случае использования - rHow to find the second minimum number without using arrays or loops - c ++get GitHub git branch for AWS CodeBuild - gitReact: rendering a concatenated component based on an imported component list - ecmascript-6Strange mixing system + homebrew Python with LLDB - pythonRxJs: list of requests from the server, consumption of values, repeated request when we are almost out of values - httpAll Articles