2

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?

tshepang
  • 12,111
  • 21
  • 91
  • 136

3 Answers3

3

Changing Android device orientation recalls your onCreate method, I believe. As a result, weird things tend to happen. There are two things that you could potentially do:

1- You can try catching the orientation change and doing some code there to try to prevent the issues that result from orientation changing:

@Override
public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        setContentView(R.layout.main);
        // etc.
}

2- Or prevent orientation-changing altogether in your manifest by adding: android:screenOrientation="portrait"
to your main activity tag, to stop this issue from reoccuring.. that is, if you are willing to prevent orientation changing.

I generally use option 2 for my apps because orientation changing tends to cause all sorts of problems.

Side note: I've seen people say they add android:configChanges="orientation|keyboardHidden" to their main activity's manifest to fix some orientation troubles as well, so that may be worth a try as well.

Mxyk
  • 10,678
  • 16
  • 57
  • 76
  • Thanks. I just wish I can leave the configs default (if user changes his screen orientation, let the apps acts as he wants to). The strange thing is this is not occurring on newer emulators (3.x, 4.x). –  Jun 01 '12 at 16:32
  • Perhaps orientation bugs were fixed in newer versions of Android? – Mxyk Jun 01 '12 at 16:39
  • Thank you for your help. I've realized that there were some bugs in my app. However, your information is really helpful. –  Jun 01 '12 at 17:24
  • Mike, I'd like to send you all my rep as a bounty but I don't know how to do that. You don't know how much time you just have saved for me. Thank you so much. I was stuck with this issue for weeks. When I was asking this question I thought if I didn't ask, I would be crazy. Thank you, very much...... –  Jun 01 '12 at 19:06
1

When the orientation changes, the actions are:

detach -> recreate the view -> attach

If the attach has not happened, getActivity() will return a null. If you are using a FragmentPagerAdapter, this is automatically taken care of. Keeping references to fragments outside of the adapter will typically cause such issues.

(The Fragment Manager will continue to keep the references and will return them to you in these cases).

Aviral
  • 1,141
  • 1
  • 10
  • 16
0

Use these links as a reference. Android Memory Leak, no static variables and Activity restart on rotation Android

I believe this is what you're asking about.

Community
  • 1
  • 1
Pjrat111
  • 213
  • 1
  • 3
  • 9