The onActivityCreated () fragment is called after the onDestroy () action

Today I noticed strange behavior in my application.

This happens when I stop my application using the device view from Eclipse. Can someone explain this?

Why is onActivityCreated() from a Fragment called even when the Activity already destroyed? MyHomeActivity contains two MyHomeActivity , and a similar log is created for both.

Here I insert the logs for one Fragment . NullPointerException is a secondary issue.

I am surprised why onActivityCreated() is called when the call stack was initiated from onDestroy() from MyHomeActivity ?

 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 use the support library to provide Fragment to pre-HoneyComb devices, if that matters.

+7
source share
1 answer

After some testing and viewing FragmentManager.moveToState, I believe that when the Fragment processed by the FragmentPagerAdapter , it is inevitable that a Fragment , which was previously merged into savedState (as part of the process of stopping the application before you kill the process from the DDMS tab in eclipse), first must be "created" (in the terminology of the FragmentManager ) before it can be destroyed,

This may be an unintended consequence of the process of restoring fragments from a saved state. When FragmentActivity onCreate and finish() called, it is assumed that FragmentActivity ceases to be configured and exits. The visual experience is that this happens, but it looks like the FragmentManager taking it upon itself to continue the life cycle for the pre-existing Fragment s, albiet with small reductions. This process apparently executes lifecycle methods before onActivityCreated , and then executes onDestroy and onDetach , passing them between them.

The best way to handle this, apparently, is to solve the secondary problems (in this case, your NPE) caused by this launch. It seems that there is an opportunity to optimize this from the Fragment life cycle, but with the r12 support library, the situation seems to be a consequence of the design.

+14
source

All Articles