Android - 保存/恢复片段状态 [英] Android - save/restore fragment state

查看:26
本文介绍了Android - 保存/恢复片段状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动,其中我浏览了几个片段.在每个片段中,我都有几个视图(EditText、ListView、Map 等).

I have an Activity in which I go through several fragments. In every fragment I have several views (EditText, ListView, Map, etc).

如何保存当时显示的片段实例?当活动为 onPause() --> 时,我需要它工作onResume().当我从另一个片段返回时(从 backstack 弹出),我也需要它工作.

How can I save the instance of the fragment that is shown at that moment? I need it to work when the activity is onPause() --> onResume(). Also I need it to work when I return from another fragment (pop from backstack).

从主 Activity 调用第一个片段,然后从片段调用下一个片段.

From the main Activity I call the first fragment, then from the the fragment I call the next one.

我的活动代码:

public class Activity_Main extends FragmentActivity{

public static Fragment_1 fragment_1;
public static Fragment_2 fragment_2;
public static Fragment_3 fragment_3;
public static FragmentManager fragmentManager;

@Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

     fragment_1 = new Fragment_1();

     fragment_2 = new Fragment_2();

     fragment_3 = new Fragment_3();

     fragmentManager = getSupportFragmentManager();
     FragmentTransaction transaction_1 = fragmentManager.beginTransaction();
     transaction_1.replace(R.id.content_frame, fragment_1);
     transaction_1.commit();
}}

然后这是我的一个片段的代码:

Then here is the code for one of my fragments:

public class Fragment_1 extends Fragment {

      private EditText title;
      private Button go_next;


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

            View rootView = inflater.inflate(R.layout.fragment_1,
            container, false);

            title = (EditText) rootView.findViewById(R.id.title);

            go_next = (Button) rootView.findViewById(R.id.go_next);

            image.setOnClickListener(new View.OnClickListener() {

         @Override
         public void onClick(View v) {

                 FragmentTransaction transaction_2 = Activity_Main.fragmentManager
                .beginTransaction();

                 transaction_2.replace(R.id.content_frame,
                  Activity_Main.fragment_2);
                 transaction_2.addToBackStack(null);
                 transaction_2.commit();  

            });
        }}

我搜索了很多信息,但都没有明确.有人可以给出一个明确的解决方案和一个例子吗?

I have searched a lot of information but nothing clear. Can somebody give a clear solution and an example, please ?

推荐答案

当一个 Fragment 被移到 backstack 时,它并没有被销毁.所有实例变量都保留在那里.所以这是保存数据的地方.在 onActivityCreated 中,您检查以下条件:

When a fragment is moved to the backstack, it isn't destroyed. All the instance variables remain there. So this is the place to save your data. In onActivityCreated you check the following conditions:

  1. 捆绑包是否为 != null?如果是,那就是保存数据的地方(可能是方向改变).
  2. 是否有数据保存在实例变量中?如果是,请从他们那里恢复您的状态(或者什么也不做,因为一切都应该如此).
  3. 否则您的片段将首次显示,请重新创建所有内容.

这是一个例子

public class ExampleFragment extends Fragment {
    private List<String> myData;

    @Override
    public void onSaveInstanceState(final Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable("list", (Serializable) myData);
    }

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

        if (savedInstanceState != null) {
            //probably orientation change
            myData = (List<String>) savedInstanceState.getSerializable("list");
        } else {
            if (myData != null) {
                //returning from backstack, data is fine, do nothing
            } else {
                //newly created, compute data
                myData = computeData();
            }
        }
    }
}

这篇关于Android - 保存/恢复片段状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆