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

2

4

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)
Millan answered 14/1, 2013 at 16:1 Comment(1)
can you should the source code?Sclerosis
M
3

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

Millan answered 14/1, 2013 at 17:7 Comment(1)
Do you know what method you were calling that relied on the activity being created?Palpitate
C
1

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.

Chitwood answered 14/1, 2013 at 16:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.