0

I currently have a fragment attached to the activity. When I rotate my device, expect the details and position of grid to be saved and restored when the view is created again. The onSavedStateInstance() runs fine and I bundle up everything, but when the onCreateView() of the fragment is invoked, the stateInstance is null. I know a similar question has been asked and answered before but it did not really help in my case. Here are some snippets that may be useful to debug.

MainActivityFragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_main, container, false);

    if (savedInstanceState != null) {
        gridViewObjects = (List<GridViewObject>) savedInstanceState.get(movieKey);
        position = savedInstanceState.getInt("position");
    } else {
        gridViewObjects = new ArrayList<>();
    }
    return view;
}

@Override
public void onSaveInstanceState(Bundle outState) {
    outState.putParcelableArrayList(movieKey, (ArrayList) gridViewObjects);
    outState.putInt("position", gridView.getFirstVisiblePosition());
    super.onSaveInstanceState(outState);
}

fragment_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="app.sunshine.android.example.com.popmovies.MainActivityFragment">

    <GridView
        android:id="@+id/fragment_main_gridView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true"
        android:gravity="center"
        android:numColumns="2"
        android:stretchMode="columnWidth"
        android:verticalSpacing="5dp" />

</FrameLayout>

MainActivity

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ActionBar actionBar = getSupportActionBar();
    actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.indigo_500)));
    setContentView(R.layout.activity_main);
}

Edit: On debug, I see that on device orientation change, the activity is re-created and the savedInstanceState in OnCreate() method is not null. The bundle details are intact. It's only when the fragment is created, the savedInstanceState in OnCreateView() is null.

Any help appreciated!

shark1608
  • 619
  • 11
  • 24

2 Answers2

0

Try this

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);    

         if (savedInstanceState != null) {
            gridViewObjects = (List<GridViewObject>) savedInstanceState.get(movieKey);
            position = savedInstanceState.getInt("position");
        } else {
            gridViewObjects = new ArrayList<>();
        }
    }

And

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_main, null);
    return view;
}
Mohammad Tauqir
  • 1,779
  • 1
  • 16
  • 49
  • Thanks but that did not help. Here's what's happening. On orientation change, the activity is created again. So on mainActivity (not the fragment) the instance is not null and I can view the details of the bundle. But when the fragment view is created (onCreateView), the savedInstanceState is null. – shark1608 Oct 03 '15 at 20:16
0

In the fragment, save instance state by override onSaveInstanceState and restore on onActivityCreated:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    ...
    if (savedInstanceState != null) {
        //Restore the fragment's state here
    }
}
...
@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

//Save the fragment's state here



}

An important point, in the activity, you have to save fragment's instance on onSaveInstanceState and restore on onCreate.

public void onCreate(Bundle savedInstanceState) {
    ...
    if (savedInstanceState != null) {
        //Restore the fragment's instance
        mContent = getSupportFragmentManager().getFragment(
                    savedInstanceState, "mContent");
        ...
    }
    ...
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

//Save the fragment's instance
getSupportFragmentManager().putFragment(outState, "mContent", mContent);



}

So to correctly save instance state of Fragment, you should do above following codes.

prat
  • 795
  • 5
  • 14
  • 1
    I know this answer is from here : http://stackoverflow.com/questions/15313598/once-for-all-how-to-correctly-save-instance-state-of-fragments-in-back-stack Could you tell me how it would work if the savedInstaceState received itself is null? I mean, I can restore the fragment(which is not what I'm looking for) only when the savedInstaceState is not null. – shark1608 Oct 03 '15 at 20:55
  • The `savedInstanceState` is null when no data was been previously saved. To save data you must override the `onSaveInstanceStateBundle(Bundle)` method as described in the Android documentation : [link](http://developer.android.com/reference/android/app/Activity.html) – prat Oct 03 '15 at 21:28
  • Yes, I understand that and that is exactly what I've done. My understanding is that I do not need to save the entire fragment state. – shark1608 Oct 04 '15 at 00:21