ConfigActivity: Save editor state across fragment instances

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Samuel Holland 2017-08-24 01:28:10 -05:00
parent e446870ac1
commit c0a76f87da

View File

@ -16,10 +16,12 @@ import com.wireguard.config.Config;
*/ */
public class ConfigActivity extends BaseConfigActivity { public class ConfigActivity extends BaseConfigActivity {
private static final String KEY_EDITOR_STATE = "editorState";
private static final String TAG_DETAIL = "detail"; private static final String TAG_DETAIL = "detail";
private static final String TAG_EDIT = "edit"; private static final String TAG_EDIT = "edit";
private static final String TAG_LIST = "list"; private static final String TAG_LIST = "list";
private Fragment.SavedState editorState;
private final FragmentManager fm = getFragmentManager(); private final FragmentManager fm = getFragmentManager();
private final FragmentCache fragments = new FragmentCache(fm); private final FragmentCache fragments = new FragmentCache(fm);
private boolean isLayoutFinished; private boolean isLayoutFinished;
@ -74,6 +76,11 @@ public class ConfigActivity extends BaseConfigActivity {
} else if (!TAG_LIST.equals(tag)) { } else if (!TAG_LIST.equals(tag)) {
final BaseConfigFragment fragment = fragments.get(tag); final BaseConfigFragment fragment = fragments.get(tag);
if (!tag.equals(visibleFragmentTag)) { if (!tag.equals(visibleFragmentTag)) {
// Restore any saved editor state the first time its fragment is added.
if (TAG_EDIT.equals(tag) && editorState != null) {
fragment.setInitialSavedState(editorState);
editorState = null;
}
final FragmentTransaction transaction = fm.beginTransaction(); final FragmentTransaction transaction = fm.beginTransaction();
if (TAG_EDIT.equals(tag) || (!isSplitLayout && TAG_DETAIL.equals(tag))) { if (TAG_EDIT.equals(tag) || (!isSplitLayout && TAG_DETAIL.equals(tag))) {
transaction.addToBackStack(null); transaction.addToBackStack(null);
@ -140,6 +147,8 @@ public class ConfigActivity extends BaseConfigActivity {
@Override @Override
public void onCreate(final Bundle savedInstanceState) { public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
if (savedInstanceState != null)
editorState = savedInstanceState.getParcelable(KEY_EDITOR_STATE);
setContentView(R.layout.config_activity); setContentView(R.layout.config_activity);
isSplitLayout = findViewById(R.id.detail_fragment) != null; isSplitLayout = findViewById(R.id.detail_fragment) != null;
mainContainer = isSplitLayout ? R.id.detail_fragment : R.id.master_fragment; mainContainer = isSplitLayout ? R.id.detail_fragment : R.id.master_fragment;
@ -203,8 +212,16 @@ public class ConfigActivity extends BaseConfigActivity {
@Override @Override
public void onSaveInstanceState(final Bundle outState) { public void onSaveInstanceState(final Bundle outState) {
// We cannot save fragments that might switch between containers if the layout changes. // We cannot save fragments that might switch between containers if the layout changes.
if (isLayoutFinished && isServiceAvailable && !isStateSaved) if (isLayoutFinished && isServiceAvailable && !isStateSaved) {
// Save the editor state before destroying it.
if (TAG_EDIT.equals(visibleFragmentTag)) {
// For the case where the activity is resumed.
editorState = fm.saveFragmentInstanceState(fragments.get(TAG_EDIT));
// For the case where the activity is restarted.
outState.putParcelable(KEY_EDITOR_STATE, editorState);
}
moveToFragment(null, isSplitLayout ? null : TAG_LIST); moveToFragment(null, isSplitLayout ? null : TAG_LIST);
}
// Prevent further changes to fragments. // Prevent further changes to fragments.
isStateSaved = true; isStateSaved = true;
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);