0

I am trying to save fragment state. I have an activity and several fragments. The sequence of actions: add first fragment, change view manually (make visibility of first LinearLayout GONE and second LinearLayout VISIBLE), detach fragment, add another one, detach it and again attach first fragment.

Adding/attaching/detaching works good but setRetainInstanse(true) saves only initial fragment state.

Finally I get first LinearLayout visible at my fragment (instead of second) so I've tried to make it by hands but it doesn't work:

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


if (savedInstanceState != null) {
            if (savedInstanceState.containsKey(BUNDLE_IS_LOADING)) {
                if (savedInstanceState.getBoolean(BUNDLE_IS_LOADING)) {
                    mBlockContent.setVisibility(View.GONE);
                    mBlockProgress.setVisibility(View.VISIBLE);

                } else {
                    mBlockContent.setVisibility(View.VISIBLE);
                    mBlockProgress.setVisibility(View.GONE);
                }
            }
        }
        }

        setRetainInstance(true);
}

@Override
public void onSaveInstanceState(Bundle b) {
    super.onSaveInstanceState(b);
    b.putBoolean(BUNDLE_IS_LOADING,
            mBlockProgress.getVisibility() == View.VISIBLE);
}

I use compatibility library rev. 11.

Solution for me:

    private boolean isProgressing;

private void saveViewsState() {
    isProgressing = mBlockProgress.getVisibility() == View.VISIBLE;
}

private void switchToProgress() {
    mBlockContent.setVisibility(View.GONE);
    mBlockProgress.setVisibility(View.VISIBLE);
}

private void switchToContent() {
    mBlockContent.setVisibility(View.VISIBLE);
    mBlockProgress.setVisibility(View.GONE);
}

@Override
public void onSaveInstanceState(Bundle b) {
    super.onSaveInstanceState(b);
    saveViewsState();
}

@Override
public void onPause() {
    super.onPause();
    saveViewsState();
}

@Override
public void onResume() {
    super.onResume();
    if (isProgressing) {
        switchToProgress();
    } else {
        switchToContent();
    }
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if (isProgressing) {
        switchToProgress();
    } else {
        switchToContent();
    }
}
Alex Zaitsev
  • 2,013
  • 4
  • 30
  • 56
  • See http://stackoverflow.com/questions/11160412/why-use-fragmentsetretaininstanceboolean and http://stackoverflow.com/questions/11182180/understanding-fragments-setretaininstanceboolean "..can only be used by Fragments *not* in the back stack..". I suspect that a *new* Fragment is being created. Look at how/when `onCreate` is called. –  Jan 17 '13 at 23:25
  • my fragments are not in back stack. It definitely are not recreated because I have an loader in fragment and when it finishes I get a Toast. – Alex Zaitsev Jan 17 '13 at 23:30
  • I found that onSaveInstanceState isn't called! Even if I remove setRetainInstanse row. I've checked similar questions. It should work but.. – Alex Zaitsev Jan 18 '13 at 15:08

0 Answers0