是否必须退出Android Lifecycle才能成为观察员? [英] Is it mandatory to remove yourself as an observer from Android Lifecycle?
本文介绍了是否必须退出Android Lifecycle才能成为观察员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在构建一个Android Java类,该类实现了 LifecycleObserver
界面.
I am building an Android Java class which implements the LifecycleObserver
interface.
这是构造函数:
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?
推荐答案
TL; DR:不会.
TL;DR: Nope.
根据此链接此处,用户在其中询问了您的问题android-lifecycles
Github回购. Google开发人员对此问题的回答是:
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.
这篇关于是否必须退出Android Lifecycle才能成为观察员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文