首页 文章

活动和片段之间的生命周期

提问于
浏览
2

在我的调试中,我注意到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 回答

  • 0

    只是猜测停止时,你从内部到外部组件停止,而开始从外部到内部 .

  • 0

    我也通过以下链接:

    你可以参考Activity onStart() being called before Fragment's onActivityCreated()

    http://developer.android.com/guide/components/fragments.html

    enter image description here

    Also it is clearly mentioned in the doc like: 图中说明了片段生命周期的流程,因为它受到主机活动的影响 .

    在此图中,您可以看到活动的每个连续状态如何确定片段可能接收的回调方法 . 例如,当活动收到onCreate()回调时,活动中的片段只接收onActivityCreated()回调 . 一旦活动达到恢复状态,您就可以自由地向活动添加和删除片段 . 因此,只有当活动处于恢复状态时,片段的生命周期才能独立地改变 .

    但是, when the activity leaves the resumed state, the fragment again is pushed through its lifecycle by the activity .

    希望这会有所帮助 .

  • 0

    为什么onResume()命令在您的activity / fragment中很重要?根据Google指南,Fragments应设计为可插拔组件,而不依赖于父Activity .

    我怀疑你可能对Fragment中的父活动有太多依赖,可能需要重构 .

    理想情况下,片段的设计应使您能够将其插入任何活动而无需任何修改 .

相关问题