我对此表示怀疑:
>这是真的吗?有人可以提供一些链接来做解释生命周期行为的文档吗?
>究竟什么是Fragment restart()?
> Android可以决定删除不可见的片段但是保留包含它们的活动吗?
注1:我测试过,由于活动重新创建而添加了Fragment时调用了onActivityCreated,并且在活动完全启动并激活后手动添加片段时也是如此.
注2:我正在使用23.3.0支持版本进行测试.某些行为是否有可能从以前的版本发生变化?
解决方法
onRestart()在你的Activity在后台堆栈中调用并且你回击按钮时,所以不需要重新创建(它只是重新启动).
Fragment中不存在一些方法(如onRestart(),onRestoreInstanceState(),不知道为什么).因此,重新启动在主机中发生并触发Fragment的onStart(),onResume(),只是认为它重新启动了Activity.
另一个有依恋和脱离的故事.在某些回调中,您希望安全地使用视图树,并且onActivityCreated()是一个很好的候选者,因此它不会重新启动.
我猜Fragment没有onRestart(),因为开发人员需要管理后端堆栈,你可以保留Fragments(不应该放在后端堆栈中).
最好的证明是代码.尝试使用日志记录.前段时间我did it myself,因为史蒂夫的项目测试只有一个片段.
推送一个小模式(启动ActivityWithDynamicFragments,然后按下后退按钮),这是一个日志:
176448881: DynamicFragment.<init> / ctor 176448881: DynamicFragment.onAttach / in base with context 176448881: DynamicFragment.onAttach / in base with activity 176448881: DynamicFragment.onAttach / out base with activity 176448881: DynamicFragment.onAttach / out base with context 176448881: DynamicFragment.onCreate / in base 176448881: DynamicFragment.onCreate / out base 176448881: DynamicFragment.onCreateView / in brand new 176448881: DynamicFragment.onCreateView / out 176448881: DynamicFragment.onViewCreated / in base 176448881: DynamicFragment.onViewCreated / out base 176448881: DynamicFragment.onActivityCreated / in base 176448881: DynamicFragment.onActivityCreated / out base 176448881: DynamicFragment.onViewStateRestored / in base 176448881: DynamicFragment.onViewStateRestored / out base 176448881: DynamicFragment.onStart / in base 176448881: DynamicFragment.onStart / out base 176448881: DynamicFragment.onResume / in base 176448881: DynamicFragment.onResume / out base 176448881: DynamicFragment.onPause / in base 176448881: DynamicFragment.onPause / out base 227303269: ActivityWithDynamicFragments.onCreate / in brand new 227303269: ActivityWithDynamicFragments.onCreate / in base 227303269: ActivityWithDynamicFragments.onCreate / out base 227303269: ActivityWithDynamicFragments.onCreate / after super 227303269: ActivityWithDynamicFragments.onContentChanged / in base 227303269: ActivityWithDynamicFragments.onContentChanged / out base 227303269: ActivityWithDynamicFragments.onStart / in base 227303269: ActivityWithDynamicFragments.onStart / out base 227303269: ActivityWithDynamicFragments.onResume / in base 227303269: ActivityWithDynamicFragments.onResume / out base 227303269: ActivityWithDynamicFragments.onPostResume / in base 227303269: ActivityWithDynamicFragments.onResumeFragments / in base 227303269: ActivityWithDynamicFragments.onResumeFragments / out base 227303269: ActivityWithDynamicFragments.onPostResume / out base 227303269: ActivityWithDynamicFragments.onAttachedToWindow / in base 227303269: ActivityWithDynamicFragments.onAttachedToWindow / out base 176448881: DynamicFragment.onSaveInstanceState / in base 176448881: DynamicFragment.onSaveInstanceState / out base 176448881: DynamicFragment.onStop / in base 176448881: DynamicFragment.onStop / out base 227303269: ActivityWithDynamicFragments.onPause / in base 227303269: ActivityWithDynamicFragments.onPause / out base D/NSA: onRestart 176448881: DynamicFragment.onStart / in base 176448881: DynamicFragment.onStart / out base 176448881: DynamicFragment.onResume / in base 176448881: DynamicFragment.onResume / out base 227303269: ActivityWithDynamicFragments.onStop / in base 227303269: ActivityWithDynamicFragments.onStop / out base 227303269: ActivityWithDynamicFragments.onDestroy / in base 227303269: ActivityWithDynamicFragments.onDestroy / out base
我们有两个术语娱乐和重启.
只有在视图树中进行修改(甚至可能是没有UI的Fragment)时,才会进行重新创建.
当您的视图树稳定时(例如,您在后栈中使用片段并且您想要将其恢复回来的活动),就会重新启动.
Can Android decide to delete not visible fragments but mantain the activity that holds them?
我相信Android操作系统杀死唯一可管理的量子是一个过程(但它是debatable).
对于Fragment,您可以查看FragmentActivity和FragmentManager的源代码(我没有看到这样的流程).