使用片段保存和恢复状态

时间:2016-12-03 15:17:53

标签: android android-fragments savestate

我试图了解使用片段保存和恢复状态的过程。我已经使用它创建了滑动导航菜单。

在其中一个片段中有以下代码:

public class FifthFragment extends Fragment {

    CheckBox cb;
    View view;

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

        view = inflater.inflate(R.layout.fifth_layout, container, false);
        cb = (CheckBox) view.findViewById(R.id.checkBox);
        return view;

    }

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

        if (savedInstanceState != null) {
            // Restore save state
        }

    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        // save state
    }

}

例如,我想在用户退出片段之前保存CheckBox的状态,并在再次创建片段时恢复它。怎么做到这一点?

编辑:

根据拉克塞尔森的回答,我已将我的片段改为:

public class FifthFragment extends Fragment {

    private static final String CHECK_BOX_STATE = "string";
    CheckBox cb;
    View view;

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

        cb = (CheckBox) view.findViewById(R.id.checkBox);
        if (savedInstanceState == null) {
            Log.i("statenull", "null");
        }
        if (savedInstanceState != null) {
            // Restore last state for checked position.
            boolean checked = savedInstanceState.getBoolean(CHECK_BOX_STATE, false);
            cb.setChecked(checked);
        }

        return view;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean(CHECK_BOX_STATE, cb.isChecked());
    }

}

我已记录I/statenull: null因此未保存savedInstanceState。我做错了什么?

2 个答案:

答案 0 :(得分:3)

您希望在@Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putBoolean(CHECK_BOX_STATE, cb.getChecked()); } 中保存当前已检查状态的值。

这样的事情:

CheckBox

然后在创建视图时,您希望获得值(如果存在)。并使用它设置@Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fifth_layout, container, false); cb = (CheckBox) view.findViewById(R.id.checkBox); if (savedInstanceState != null) { // Restore last state for checked position. boolean checked = savedInstanceState.getBoolean(CHECK_BOX_STATE, false); cb.setChecked(checked); } return view; } 州。

private void setFragment(String tag, Fragment newFragment) {
   FragmentManager fm = getSupportFragmentManager();
   Fragment savedFragment = fm.getFragmentByTag(tag);
   fm.replace(R.id.container, savedFragment != null ? savedFragment : newFragment, tag);
   fm.commit();
}

编辑:

添加片段时,请务必使用标记或ID添加片段,以便检索相同的实例。

您可以执行辅助方法来检索片段并设置片段。

switch

所以你的switch (position) { case 0: setFragment("A", new FragmentA()); break; .... } 可以改为调用辅助方法。

switch case

注意:这只是一个示例不是最佳做法,因为您现在每次都在UITableView创建新的片段。但它可能会指出你正确的方向。

答案 1 :(得分:0)

查看所有示例之后。这是保存片段状态的解决方案:

两个步骤:

1。

String saveValue;
 
 
 @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState == null) {
            saveValue = "";
        } else {

            saveValue = savedInstanceState.getString("saveInstance");
          

        }
    }
@Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        //save the values of fragment if destroy on second to back
        if (!saveValue.isEmpty())
            savedInstanceState.putString("saveInstance", saveValue);
       
    }

onSaveInstanceState 中,您可以保存值。在销毁碎片之后,您可以通过 onCreate 接收您的值。