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

∥☆過路亽.° 提交于 2019-12-13 14:22:34

问题


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)

回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/14322014/how-is-my-fragments-oncreateview-called-before-its-oncreate-when-finishing-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!