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

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

问题描述

我有一个活动中,我通过几个片段。在每一个片段,我有几个观点(EditText上,ListView控件,地图等)。

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

我如何保存显示在那一刻片段的实例?我需要它来工作,当活动的onPause() - > onResume()。此外,我需要它的时候,我从另一个片段返回(POP从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).

从主活动我所说的第一个片段,然后从我所说的下一个片段。

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

code我的活动:

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

然后这里是code为我的片段之一:

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 ?

推荐答案

当一个片段被移到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天全站免登陆