[英]Fragment's onActivityCreated() is called after onDestroy() of Activity
Today I noticed a strange behavior in my application. 今天我发现我的应用程序中有一个奇怪的行为
It happens when I stop my application using Devices view from Eclipse. 当我使用Eclipse的Devices视图停止我的应用程序时会发生这种情况。 Can someone explain it?
有人可以解释一下吗?
Why is onActivityCreated()
of Fragment
called even when Activity
is already destroyed? 为什么即使
Activity
已被销毁,也会调用Fragment
onActivityCreated()
? MyHomeActivity
contains two Fragment
s and similar log is generated for both. MyHomeActivity
包含两个Fragment
并为两者生成类似的日志。
Here I am pasting logs for one Fragment
. 我在这里粘贴一个
Fragment
日志。 NullPointerException
is a secondary problem. NullPointerException
是次要问题。
I am surprised why onActivityCreated()
is called when the call stack was initiated from onDestroy()
of MyHomeActivity
? 我很惊讶为什么在从
MyHomeActivity
onDestroy()
启动调用堆栈时调用onActivityCreated()
?
03-05 12:31:21.414: W/System.err(5638): java.lang.NullPointerException
03-05 12:31:21.421: W/System.err(5638): at **MyListViewFrag.onActivityCreated**(BuddyListViewFrag.java:85)
03-05 12:31:21.421: W/System.err(5638): at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1468)
03-05 12:31:21.421: W/System.err(5638): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:931)
03-05 12:31:21.421: W/System.err(5638): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
03-05 12:31:21.421: W/System.err(5638): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1070)
03-05 12:31:21.421: W/System.err(5638): at android.support.v4.app.FragmentManagerImpl.dispatchReallyStop(FragmentManager.java:1888)
03-05 12:31:21.421: W/System.err(5638): at android.support.v4.app.FragmentActivity.onReallyStop(FragmentActivity.java:787)
03-05 12:31:21.421: W/System.err(5638): at android.support.v4.app.FragmentActivity.doReallyStop(FragmentActivity.java:764)
03-05 12:31:21.421: W/System.err(5638): at android.support.v4.app.FragmentActivity.onDestroy(FragmentActivity.java:322)
03-05 12:31:21.421: W/System.err(5638): at MyFragmentActivity.onDestroy(RbrFragmentActivity.java:57)
03-05 12:31:21.421: W/System.err(5638): at **MyHomeActivity.onDestroy**(MyHomeActivity.java:254)
03-05 12:31:21.421: W/System.err(5638): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:2663)
03-05 12:31:21.421: W/System.err(5638): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:2694)
03-05 12:31:21.421: W/System.err(5638): at android.app.ActivityThread.access$2100(ActivityThread.java:117)
03-05 12:31:21.421: W/System.err(5638): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:968)
03-05 12:31:21.421: W/System.err(5638): at android.os.Handler.dispatchMessage(Handler.java:99)
03-05 12:31:21.421: W/System.err(5638): at android.os.Looper.loop(Looper.java:130)
03-05 12:31:21.421: W/System.err(5638): at android.app.ActivityThread.main(ActivityThread.java:3687)
03-05 12:31:21.429: W/System.err(5638): at java.lang.reflect.Method.invokeNative(Native Method)
03-05 12:31:21.429: W/System.err(5638): at java.lang.reflect.Method.invoke(Method.java:507)
03-05 12:31:21.429: W/System.err(5638): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
03-05 12:31:21.429: W/System.err(5638): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
03-05 12:31:21.429: W/System.err(5638): at dalvik.system.NativeStart.main(Native Method)
I am using support library for providing Fragment
s to pre-HoneyComb devices, if that makes any difference. 我正在使用支持库为预装HoneyComb设备提供
Fragment
,如果这有任何区别的话。
After some testing and reviewing FragmentManager.moveToState , I believe that when the Fragment
is being handled by a FragmentPagerAdapter
, it is unavoidable that a Fragment
which was previously coalesced into savedState (as part of the process of stopping the app before you kill the process from the DDMS tab in eclipse), must first be "created" (in the terminology of FragmentManager
) before it can be destroyed. 在对FragmentManager.moveToState进行一些测试和检查之后,我相信当
Fragment
由FragmentPagerAdapter
处理时,以前合并到savedState的Fragment
是不可避免的(作为在你杀死进程之前停止应用程序的过程的一部分)在eclipse中的DDMS选项卡,必须先被“创建”(在FragmentManager
的术语中)才能被销毁。
This may actually be an unintended consequence of the process of rebuilding the fragments from saved state. 这实际上可能是从已保存状态重建片段的过程的意外后果。 When the
FragmentActivity
is executing onCreate
and finish()
is called, the intention is that the FragmentActivity
stops setting up and exits. 当
FragmentActivity
正在执行onCreate
并且调用了finish()
,意图是FragmentActivity
停止设置和退出。 The visual experience is that this occurs, but it seems like the FragmentManager
takes it on itsself to continue the lifecycle for previously existing Fragment
s, albiet with some short cuts. 视觉体验是这种情况发生的,但似乎
FragmentManager
自己继续生存以前存在的Fragment
s,albiet和一些捷径。 This process seems to execute the lifecycle methods up to onActivityCreated
and then execute onDestroy
and onDetach
, skipping those in between. 这个过程似乎执行生命周期方法直到
onActivityCreated
,然后执行onDestroy
和onDetach
,跳过它们之间的那些。
The best way to handle this appears to be to address the secondary issues (in this case, your NPE) caused by this startup. 处理此问题的最佳方法似乎是解决此启动导致的次要问题(在本例中为您的NPE)。 It seems like there would be room to optimize this out of the
Fragment
lifecycle, but with the r12 support library, the situation appears to be a consequence of the design. 看起来在
Fragment
生命周期中有优势可以进行优化,但是对于r12支持库,情况似乎是设计的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.