2

The Fragments in my app are not re-added to the activity's view when the device is rotated. If i understand the documentation (and similar questions here on SO) correctly, this should be handled be the fragment manager (without having to specify android:configChanges in my manifest).

My Activity looks like:

public class MainActivity extends SherlockFragmentActivity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        if (savedInstanceState == null){
            getSupportFragmentManager().beginTransaction()
                .add(R.id.main_fragment_placeholder, new DashboardFragment(),"dashboard")
                .commit();
        }
    }
}

And a Fragment looks like (omitted clicklisteners and so on for clarity):

public class DashboardFragment extends SherlockFragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_dashboard, container, false);
    }
}

The layout files look like (activity_main.xml):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:id="@+id/main.main">

    <FrameLayout android:id="@+id/main_fragment_placeholder"
             android:layout_height="fill_parent"
             android:layout_weight="1"
             android:layout_width="0px"
             />

</LinearLayout>

And fragment_layout.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main.wrapper"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <my.layout.DashboardLayout
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:id="@+id/main.dashboard">
    <!-- some buttons here -->
    </my.layout.DashboardLayout>

</LinearLayout>

However, when i rotate my device the screen always becomes blank.

sgdesmet
  • 628
  • 6
  • 14

2 Answers2

11

Turns out I did a Stupid Thing. I overrode the onSaveInstanceState(...) method in my activity, without calling super.onSaveInstanceState(), which resulted in the fragments not being recreated. Posting the answer here in hopes that I can save somebody else the time!

sgdesmet
  • 628
  • 6
  • 14
  • Thank you! I was using the [CompositeAndroid](https://github.com/passsy/CompositeAndroid) library and it's `CompositeActivity` as a base class for my activity and it didn't call `super.onSaveInstanceState()` in the `CompositeActivity` class :'( Took me 2 hours to figure that out... – Markus Ressel Feb 22 '18 at 00:01
0

Fragments usually get recreated on configuration change. If you don't wish this to happen, use

setRetainInstance(true); in the Fragment's constructor(s)

This will cause fragments to be retained during configuration change.

http://developer.android.com/reference/android/app/Fragment.html#setRetainInstance(boolean)

Just Variable
  • 892
  • 10
  • 19
  • The problem is that the fragment doesn't get recreated on configuration change (which is what i want), even with setRetainInstance(false). I just get a blank screen after re-orientation (and the fragment's methods like onCreate(...) are not being called) – sgdesmet Oct 25 '12 at 09:25
  • You might be replacing the fragment every time `onCreate()` is called. Instead, only add/replace the fragment if `savedInstanceState()` is `null`. If it is not `null`, you are coming back from a configuration change, and your existing fragments will be recreated (or, if they were retained, they are already there). `setRetainInstance(true)` means that the fragment *itself* will be retained across configuration changes, instead of being destroyed/recreated like the activity is. However, it will still be called with `onCreateView()`. – Just Variable Oct 25 '12 at 10:44
  • 1
    As you can see from the code, I check for savedInstanceState == null before adding a fragment. However, the problem is that existing fragments are NOT re-created when the activity is recreated. – sgdesmet Oct 25 '12 at 11:23
  • I'm having the same issue reported as sgdesmet, but on a FragmentSatePagerAdapter. Trying to solve an OOM and it's killing my head off. – Henrique de Sousa Sep 10 '14 at 16:37