Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdated Fragment view state lost when using setRetainInstance(true)
    primarykey
    data
    text
    <p>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.</p> <p><em>Adding/attaching/detaching works good but setRetainInstanse(true) saves only initial fragment state.</em></p> <p>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:</p> <pre><code>@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); } </code></pre> <p>I use compatibility library rev. 11.</p> <p><strong>Solution for me:</strong></p> <pre><code> 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(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload