在我的调试中,我注意到onPause / onStart / onStop / onDestroy首先在片段上调用然后在其活动上调用,而onResume则相反 - 活动的onResume在其片段之前被调用 .
为什么onPause / onStart / onStop的顺序与on活动和片段之间的onResume不同?
09-28 15:26:40.869 30320-30320/testintent D/TestFragment﹕ onStart
09-28 15:26:40.869 30320-30320/testintent D/TestActivity﹕ onStart
09-28 15:26:40.869 30320-30320/testintent D/TestActivity﹕ onResume
09-28 15:26:40.869 30320-30320/testintent D/TestFragment﹕ onResume
09-28 15:26:40.869 30320-30320/testintent D/TestActivity﹕ onPostResume
3 回答
只是猜测停止时,你从内部到外部组件停止,而开始从外部到内部 .
我也通过以下链接:
你可以参考Activity onStart() being called before Fragment's onActivityCreated()
http://developer.android.com/guide/components/fragments.html
Also it is clearly mentioned in the doc like: 图中说明了片段生命周期的流程,因为它受到主机活动的影响 .
但是, when the activity leaves the resumed state, the fragment again is pushed through its lifecycle by the activity .
希望这会有所帮助 .
为什么onResume()命令在您的activity / fragment中很重要?根据Google指南,Fragments应设计为可插拔组件,而不依赖于父Activity .
我怀疑你可能对Fragment中的父活动有太多依赖,可能需要重构 .
理想情况下,片段的设计应使您能够将其插入任何活动而无需任何修改 .