FragmentActivity doesn't see activity in onActivityCreate(), after screen orientation changed

Posted by hai bison on Stack Overflow See other posts from Stack Overflow or by hai bison
Published on 2012-06-01T16:24:43Z Indexed on 2012/06/01 16:40 UTC
Read the original article Hit count: 222

I'm using Android support library v13. There is a strange thing I couldn't understand.

When creating new activity, I load fragment as:

Main activity layout:

...
<FrameLayout
    android:id="@+id/fragment_1"
    ... />

In onCreate() of main activity:

mFragment = (FragmentActivity) getSupportFragmentManager().findFragmentById(R.id.fragment_1);
// if screen orientation changed, no need to create new instance of fragment
if (mFragment == null) {
    mFragment = ...; // create new instance of fragment

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.fragment_1, mFragment);
    // because this is called ONCE, we can use this method
    ft.commitAllowingStateLoss();
}

Now, everything works perfectly in emulators 1.5, 1.6 and 2.2. I have a phone 2.2.2.

But there is an exception: if the app is running, and screen orientation changed. Inside onActivityCreated(), getActivity() sometimes returns null. This only happens in emulators 1.5/ 1.6/ 2.2.

My phone 2.2.2 works very well, I test hundreds of times but never catch that bug. Even other emulators 3.x, 4.x work well too. Unfortunately I don't have phone 1.5/ 1.6/ 2.2.

So did you have experience with this? Is that a bug of the support library, or emulators? Could you please help me?

Thank you in advance.

© Stack Overflow or respective owner

Related posts about android

Related posts about library