使用片段保存和恢复状态

编程入门 行业动态 更新时间:2024-10-27 21:18:06
本文介绍了使用片段保存和恢复状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I'm trying to understand the process of saving and restoring state using fragments. I've created sliding navigation menu using it.

其中一个片段中包含以下代码:

In one of the fragments there is this code:

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的状态,并在再次创建片段时将其还原.如何实现呢?

For example I want to save the state of the CheckBox before user exits the fragment and restore it when the fragment is created again. How to achieve this?

根据raxellson的回答,我已将片段更改为:

According to raxellson's answer I've changed my fragment to this:

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 ,所以saveInstanceState未保存.我在做什么错了?

I got logged I/statenull: null so savedInstanceState was not saved. What am I doing wrong?

推荐答案

您要将当前选中状态的值保存在 onSaveInstanceState 中.

You want to save the value of your current checked state in onSaveInstanceState.

类似这样的东西:

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

,然后在创建视图时要获取值(如果存在).并设置它的 CheckBox 状态.

and then when your view is created you want to get the value if it's present. And set your CheckBox state with it.

@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; }

添加片段时,请确保添加带有标签或ID的片段,以便您可以检索相同的实例.

When you add the fragment, make sure to add it with a tag or id so that you can retrieve the same instance.

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

You could do a helper method to retrieve fragment and set the fragment.

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); fmmit(); }

因此您可以通过 switch 来调用helper方法.

so you your switch you can call the helper method instead.

switch (position) { case 0: setFragment("A", new FragmentA()); break; .... }

注意:这只是不是最佳示例,因为无论如何现在每次都在 switch case 中创建新的片段.但这可能会为您指明正确的方向.

Note: This is just an example not best practice since you are creating new fragments every time in your switch case now anyways. But it might point you in the right direction.

更多推荐

使用片段保存和恢复状态

本文发布于:2023-10-26 11:36:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1530024.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:片段   状态

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!