我有一个singleInstance Activity和一个片段,我在onCreate()方法中实例化,并将其添加到一个FrameLayout容器中放入活动的布局中。该活动除了打印日志外不做任何事情。 我正在使用android-support-v4 lib和android 2.3.3。单个实例活动的Android片段生命周期
我观察到一个奇怪的生命周期行为与此设置,我不知道你是否可以帮我解释这一点。我将提供日志的生命周期:
活动的第一次调用:
07-07 15:12:17.990 V/FragActivity(2358): onCreate >> [email protected]
07-07 15:12:21.010 V/FragActivity(2358): onCreate <<
07-07 15:12:21.020 V/LayoutFragment(2358): onAttach > LayoutFragment{44f467c8 #0 id=0x7f070000}
07-07 15:12:24.021 V/LayoutFragment(2358): onAttach <
07-07 15:12:24.021 V/LayoutFragment(2358): onCreate > LayoutFragment{44f467c8 #0 id=0x7f070000}
07-07 15:12:27.020 V/LayoutFragment(2358): onCreate <
07-07 15:12:27.020 V/LayoutFragment(2358): onCreateView > LayoutFragment{44f467c8 #0 id=0x7f070000}
07-07 15:12:30.022 V/LayoutFragment(2358): onCreateView <
07-07 15:12:30.030 V/LayoutFragment(2358): onActivityCreated > LayoutFragment{44f467c8 #0 id=0x7f070000}
07-07 15:12:33.030 V/LayoutFragment(2358): onActivityCreated <
07-07 15:12:33.030 V/LayoutFragment(2358): onStart > LayoutFragment{44f467c8 #0 id=0x7f070000}
07-07 15:12:36.030 V/LayoutFragment(2358): onStart <
07-07 15:12:36.040 V/FragActivity(2358): onStart > [email protected]
07-07 15:12:39.041 V/FragActivity(2358): onStart <
07-07 15:12:39.041 V/LayoutFragment(2358): onStop > LayoutFragment{44f467c8 #0 id=0x7f070000}
07-07 15:12:42.040 V/LayoutFragment(2358): onStop <
07-07 15:12:42.040 V/FragActivity(2358): onResume > [email protected]
07-07 15:12:45.041 V/FragActivity(2358): onResume <
07-07 15:12:45.041 V/LayoutFragment(2358): onStart > LayoutFragment{44f467c8 #0 id=0x7f070000}
07-07 15:12:48.040 V/LayoutFragment(2358): onStart <
07-07 15:12:48.040 V/LayoutFragment(2358): onResume > LayoutFragment{44f467c8 #0 id=0x7f070000}
07-07 15:12:51.042 V/LayoutFragment(2358): onResume <
第一个问题:为什么是的onStop()片段的创建活动过程中的方法?片段在屏幕上显示正常。
之后,我通过触发意图重新启动活动,导致活动的onNewIntent()lidecycle方法。
07-07 15:13:17.220 V/LayoutFragment(2358): onPause > LayoutFragment{44f467c8 #0 id=0x7f070000}
07-07 15:13:20.220 V/LayoutFragment(2358): onPause <
07-07 15:13:20.230 V/FragActivity(2358): onPause > [email protected]
07-07 15:13:23.231 V/FragActivity(2358): onPause <
07-07 15:13:23.231 V/FragActivity(2358): onNewIntent > [email protected]
07-07 15:13:26.231 V/FragActivity(2358): onNewIntent <
07-07 15:13:26.231 V/FragActivity(2358): onResume > [email protected]
07-07 15:13:29.230 V/FragActivity(2358): onResume <
第二个问题:为什么片段的onResume()方法不被调用?它仍然可以在屏幕上看到。据我了解活动和生命周期方法应当齐头并进......
后,我重新启动活动第二次:
07-07 15:13:42.140 V/FragActivity(2358): onPause > [email protected]
07-07 15:13:45.143 V/FragActivity(2358): onPause <
07-07 15:13:45.143 V/FragActivity(2358): onNewIntent > [email protected]
07-07 15:13:48.144 V/FragActivity(2358): onNewIntent <
07-07 15:13:48.150 V/FragActivity(2358): onResume > [email protected]
07-07 15:13:51.151 V/FragActivity(2358): onResume <
现在fragmen的生命周期方法不会被触发,在所有.. 那个怎么样?
您的我的英雄:)我认为值得一提的是,您不必将源代码导入到您的项目中,因为所需成员的可见性被设置为默认值。只需在你的src和一个扩展FragmentActivity的类中创建一个android.support.v4.app包。这足以提供对mResumed和mFragments的访问。 – dskinner