Restore instanceState of rotated background Fragment

217 views Asked by At

I have an Activity with a FrameLayout. First I load Fragment1, if the user now presses "Next" in the ActionBar I replace Fragment1 with Fragment2. (Done by the ActivityPresenter)

If I now rotate the Screen with Fragment2 loaded the "SaveInstanceState" of Fragment1 and Fragment2 is called and Fragment2 gets recreated. But not Fragment1! When I rotate back all of my Information from Fragment1 gets lost.

Activity holding the Fragments:

public class Activity1 extends ActivityBase {
private static final String TAG = Activity1 .class.getSimpleName();

private IActivityPresenter mActivityPresenter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_quote);

    if (savedInstanceState != null) {
        LogHelper.info(TAG, "onCreate --> Restored");
        mActivityPresenter= (IActivityPresenter) savedInstanceState.getSerializable("presenter");
    } else {
        LogHelper.info(TAG, "onCreate --> New");
        mActivityPresenter= new ActivityPresenter(this);
    }
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    LogHelper.info(TAG, "onSaveInstanceState");
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putSerializable("presenter", mActivityPresenter);
}

    @Override
public void loadFragment1() {
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    Fragment1 fragment1 = new Fragment1();
    ft.replace(R.id.frameLayoutContent, fragment1);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.commit();
}

@Override
public void loadFragment2() {
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    Fragment2 fragment2 = new Fragment2();
    ft.replace(R.id.frameLayoutContent, fragment2);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.addToBackStack("fragment2");
    ft.commit();
}

Fragment1 / 2:

public class Fragment1 extends Fragment {
    private static final String TAG = Fragment1.class.getSimpleName();

    private IPresenter1 mPresenter1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)                  {
        View rootView = inflater.inflate(R.layout.fragment_quote_basics, container, false);

        if (savedInstanceState != null) {
            LogHelper.info(TAG, "onCreate --> Restored");
            mPresenter1 = (IPresenter1) SavedInstanceState.getSerializable("presenter");
        } else {
            LogHelper.info(TAG, "onCreate --> New");
            mPresenter1 = new Presenter1(this);
        }

        return rootView;
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    LogHelper.info(TAG, "onSaveInstanceState");
    super.onSaveInstanceState(savedInstanceState);

    if(savedInstanceState.getSerializable("presenter") == null){
        savedInstanceState.putSerializable("presenter", mPresenter1);
    }
}

MyFragment2 looks the same as Fragment 1!

Logcat:

12-01 10:22:24.096  26226-26226/at.itpro.mobile.android.lightning I/Activity﹕ onCreate --> New
12-01 10:22:24.176  26226-26226/at.itpro.mobile.android.lightning I/Fragment1﹕ onCreate --> New
[Push button 'Next']
12-01 10:22:31.293  26226-26226/at.itpro.mobile.android.lightning I/Fragment2﹕ onCreate --> New
[Rotate the screen for the first Time]
12-01 10:22:35.508  26226-26226/at.itpro.mobile.android.lightning I/Activity﹕ onSaveInstanceState
12-01 10:22:35.518  26226-26226/at.itpro.mobile.android.lightning I/Fragment1﹕ onSaveInstanceState
12-01 10:22:35.518  26226-26226/at.itpro.mobile.android.lightning I/Fragment2﹕ onSaveInstanceState
12-01 10:22:35.518  26226-26226/at.itpro.mobile.android.lightning I/Fragment1﹕ onDestroy
12-01 10:22:35.518  26226-26226/at.itpro.mobile.android.lightning I/Fragment2﹕ onDestroy
12-01 10:22:35.518  26226-26226/at.itpro.mobile.android.lightning I/Activity﹕ onDestroy
12-01 10:22:35.588  26226-26226/at.itpro.mobile.android.lightning I/Activity﹕ onCreate --> Restored
12-01 10:22:35.598  26226-26226/at.itpro.mobile.android.lightning I/Fragment2﹕ onCreate --> Restored
[Rotate the screen for the second Time]
12-01 10:22:38.231  26226-26226/at.itpro.mobile.android.lightning I/Activity﹕ onSaveInstanceState
12-01 10:22:38.241  26226-26226/at.itpro.mobile.android.lightning I/Fragment1﹕ onSaveInstanceState
12-01 10:22:38.241  26226-26226/at.itpro.mobile.android.lightning I/Fragment2﹕ onSaveInstanceState
12-01 10:22:38.241  26226-26226/at.itpro.mobile.android.lightning I/Fragment1﹕ onDestroy
12-01 10:22:38.241  26226-26226/at.itpro.mobile.android.lightning I/Fragment2﹕ onDestroy
12-01 10:22:38.241  26226-26226/at.itpro.mobile.android.lightning I/Activity﹕ onDestroy
12-01 10:22:38.281  26226-26226/at.itpro.mobile.android.lightning I/Activity﹕ onCreate --> Restored
12-01 10:22:38.291  26226-26226/at.itpro.mobile.android.lightning I/Fragment2﹕ onCreate --> Restored
[Press back]
12-01 10:28:31.107  26226-26226/at.itpro.mobile.android.lightning I/Fragment1﹕ onCreate --> Restored

Because Fragment1 does not get created after the first rotation, the mPresenter1 is null when the screen gets rotated a secondTime. So saveInstanceState saves null. When Fragment1 now gets created/restored after back is pressed (last line of the log) the presenter is null.

Edit / Question2: The more I think of it the less it makes sense, that Fragment1 gets destroyed (on second screen rotation), although it is already destroyed / replaced?!

0

There are 0 answers