简体   繁体   中英

How is my fragment's onCreateView called before it's onCreate when finishing the activity in onCreate?

This is very odd. I have a simple app which once logged in shows a fragment in an activity. The app also has an inactivity "timeout" after which time it finishes the activity and shows the login screen -- if the app is in the background when the timeout occurs then when the next onCreate or onStart event occurs in the activity it is finished.

However sometimes when returning to the logged in activity I get a NPE in the onActivityCreated method of my fragment. The fragment is very simple and simply calls a method on a String. The string itself is retrieved through a call to getArguments(). I have verified that whenever the Fragment is created the arguments are always set.

So it seems somehow the onActivityCreated is actually called before onCreate in the fragment, which I know should be impossible. Below is a portion of the stack trace:

01-14 15:34:37.176: E/AndroidRuntime(3272):     at com.example.app.fragment.MyFragment.onActivityCreated(MyFragment.java:203)
01-14 15:34:37.176: E/AndroidRuntime(3272):     at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1468)
01-14 15:34:37.176: E/AndroidRuntime(3272):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:931)
01-14 15:34:37.176: E/AndroidRuntime(3272):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
01-14 15:34:37.176: E/AndroidRuntime(3272):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1070)
01-14 15:34:37.176: E/AndroidRuntime(3272):     at android.support.v4.app.FragmentManagerImpl.dispatchReallyStop(FragmentManager.java:1888)
01-14 15:34:37.176: E/AndroidRuntime(3272):     at android.support.v4.app.FragmentActivity.onReallyStop(FragmentActivity.java:787)
01-14 15:34:37.176: E/AndroidRuntime(3272):     at android.support.v4.app.FragmentActivity.doReallyStop(FragmentActivity.java:764)
01-14 15:34:37.176: E/AndroidRuntime(3272):     at android.support.v4.app.FragmentActivity.onDestroy(FragmentActivity.java:322)
01-14 15:34:37.176: E/AndroidRuntime(3272):     at com.actionbarsherlock.app.SherlockFragmentActivity.onDestroy(SherlockFragmentActivity.java:261)
01-14 15:34:37.176: E/AndroidRuntime(3272):     at com.example.app.MyActivity.onDestroy(MyActivity.java:195)
01-14 15:34:37.176: E/AndroidRuntime(3272):     at android.app.Activity.performDestroy(Activity.java:5273)
01-14 15:34:37.176: E/AndroidRuntime(3272):     at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1110)
01-14 15:34:37.176: E/AndroidRuntime(3272):     at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3562)

After some more debugging I have discovered the exception occurs because of a use of getActivity() before onActivityCreated() is called -- moreover a method is called on getActivity() and the method relies upon the activity having been created. /idiot

Perhaps you are storing the string in such a way that it is not saved when the fragment goes into the background? Without code that's the only guess I can offer.

One way to fix your problem would be to simply do a null check in your onActivityCreated method. Depends on how you want to handle it though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM