LifecycleRegistry实例何时开始侦听LifecycleOwner的生命周期更改? [英] When does a LifecycleRegistry instance start listening to LifecycleOwner's lifecycle changes?
问题描述
我已经开始学习架构组件,但是找不到一件事.
I've started learning architecture components, but can't find one thing.
LifecycleFragment 只会创建一个新的 LifecycleRegistry 对象,该对象不会开始观察片段的生命周期.
LifecycleFragment just creates a new LifecycleRegistry object, which does not start observing the fragment's lifecycle.
I guess the LifecycleRegistry object starts listening to the fragment's lifecycle when we, for example, put it into LiveData.observe() as first param, but I haven't found any proof of this in source code.
问题: LifecycleRegistry 对象的时间和方式开始观察片段的生命周期并刷新LifecycleRegistry.mState
?
Question: When and how does a LifecycleRegistry object start to observe a fragment's lifecycle and refresh LifecycleRegistry.mState
?
推荐答案
有一个名为LifecycleRuntimeTrojanProvider
的ContentProvider
,它已合并到应用程序的AndroidManifest.xml
中.在其onCreate
方法中,它初始化了一个称为LifecycleDispatcher
的单例,该单例负责更新所有LifecycleRegistry
实例.
There is a ContentProvider
called LifecycleRuntimeTrojanProvider
that is merged into the app's AndroidManifest.xml
. In its onCreate
method it initializes a singleton called LifecycleDispatcher
, which is responsible for updating all LifecycleRegistry
instances.
LifecycleDispatcher
使用自API 14以来就一直存在的Application.registerActivityLifecycleCallbacks 方法在创建新活动时得到通知.此时,它将ReportFragment
的实例注入到活动中. ReportFragment
如有必要,使用Fragment
生命周期回调来更新活动的LifecycleRegistry
,如下所示:
LifecycleDispatcher
uses the Application.registerActivityLifecycleCallbacks method that has been around since API 14 to get notified when a new activity is created. At this point it injects an instance of ReportFragment
into the activity. The ReportFragment
uses the Fragment
lifecycle callbacks to update the activity's LifecycleRegistry
if necessary, like this:
@Override
public void onStop() { // Showing onStop as example
super.onStop();
dispatch(Lifecycle.Event.ON_STOP);
}
private void dispatch(Lifecycle.Event event) {
if (getActivity() instanceof LifecycleRegistryOwner) {
((LifecycleRegistryOwner) getActivity()).getLifecycle().handleLifecycleEvent(event);
}
}
如果新活动是FragmentActivity
,则LifecycleDispatcher
调用
If the new activity is a FragmentActivity
, the LifecycleDispatcher
calls FragmentManager.registerFragmentLifecycleCallbacks to get notified of the activity's fragments lifecycle events. It relays the onFragmentCreated
, onFragmentStarted
and onFragmentResumed
callbacks to the LifecycleRegistry
in case the fragment is a LifecycleRegistryOwner
, in the same way as before.
在片段上调用相应的回调之后,将调用onFragmentPaused
,onFragmentStopped
和onFragmentDestroyed
回调,但是必须在之前调用LifecycleObserver
回调.因此,无论何时创建片段,LifecycleDispatcher
都会将LifecycleDispatcher.DestructionReportFragment
的实例注入其中. DestructionReportFragment
的生命周期回调用于为暂停,停止和销毁事件更新注册表.
The onFragmentPaused
, onFragmentStopped
, and onFragmentDestroyed
callbacks are called after the corresponding callbacks are called on the fragment, but the LifecycleObserver
callbacks must be called before. So whenever a fragment is created, the LifecycleDispatcher
injects an instance of LifecycleDispatcher.DestructionReportFragment
into it. The DestructionReportFragment
's lifecycle callbacks are used to update the registry for the pause, stop and destroy events.
我无法链接到该代码,因为它尚未发布,但是将库添加到项目后,您可以在Android Studio中浏览它.
I can't link to the code because it hasn't been released yet, but you can browse it in Android Studio after you add the library to your project.
这篇关于LifecycleRegistry实例何时开始侦听LifecycleOwner的生命周期更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!