如何留住片段之间的数据 [英] how to retain data between Fragments

查看:107
本文介绍了如何留住片段之间的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的3个片段A,B,C与Tabhost片段活动

I am using 3 Fragments A,B,C with Tabhost Fragment Activity

破片A,B,C各包含一些领域属于一个类只。那是说Employee类

Frag A,B,C each contains some fields belongs to one class only .That is Say Employee class

一个包含EMpnm,EmpAge,EMPNO
B包含EMpJoinDt,EmpSal
C包含EmpHobby,EmpPersonalDtls

A contains EMpnm,EmpAge,EmpNo B contains EMpJoinDt,EmpSal C contains EmpHobby,EmpPersonalDtls

导航是A-> B->下使用下一个按钮

navigation is A->B->C using next button

Employee类是公共使用的getter / setter。数据FRM的每个片段设置为Employee类
每个片段将有员工EMP =新员工()对象

Employee class is public .data frm each fragment set to Employee class using getter/Setter Each fragment will have Employee emp= new Employee() object

所以每次数据获取歼灭从破片下一步按钮点击B以便数据硝基甲苯获得通过EMP对象持有如何在对提交全班员工取得提交最终从片段1的数据保留2〜3个,使与值DB

so every time data get wipe out on next button click from Frag A to B so data dnt get hold by emp object how to retain data from fragment 1 to 2 to 3 so that at the end on submit whole Class employee get submitted with values to DB

共享preferences最适合的IT解决方案?
这是我的情况很好的解决方案?使用捆绑

片段之间[链接] 将数据传递
注意:每个片断同一类

[Link]Pass data between fragments Note:the same class for each fragment

推荐答案

共享preferences绝对不是一个好的解决方案。它被设计为基本数据类型键/值数据存储。还应该相当用于应用程序广泛preferences而不是缓存的业务数据。

SharedPreferences is definitely not a good solution. It is designed to be a key/value data store for basic data types. Also it should be rather used for application wide preferences rather than cache for business data.

preferably你应该存储在承载你所有的片段活动的共同目标,并保存/通过的onSaveInstanceState / onRestoreInstanceState 技工恢复。

Preferably you should store the common object in the Activity that hosts all your fragments, and save/restore it via onSaveInstanceState/onRestoreInstanceState mechanics.

另外,您可以创建一个UI少片段来保存共享数据。你会在你的活动初始化其他片段之前添加此片段。

Alternatively you can create a UI-less fragment to hold the shared data. You would add this fragment in your activity before initializing other fragments.

然后就可以通过访问这些片段getFragmentManager()。findFragmentByTag(YOUR_TAG)。在这种情况下,你需要将片段中保存的对象实例状态为$ P在配置更改$ pvent态损耗。

Then you can access such fragment via getFragmentManager().findFragmentByTag("YOUR_TAG). In this case you will need to save your objects instance state within fragment to prevent state loss on configuration changes.

活动的简单的概念作为数据持有人:

Simple concept of Activity as the data holder:

public interface EmployeeDataHolder {
    EmployeeData getEmployeeData();
}

public class ExampleActivity extends Activity implements EmployeeDataHolder {
  private EmployeeData mEmployeeData;

  @Override
  public EmployeeData getEmployeeData() {
    return mEmployeeData;
  }
}

public class ExampleFragment extends Fragment{

  EmployeeDataHolder mEmployeeDataHolder;

  @Override
  public void onAttach(Activity activity) {
    super.onAttach(activity);
    if (activity instanceof EmployeeDataHolder) {
      mEmployeeDataHolder = (EmployeeDataHolder) activity;
    } else 
      throw new IllegalStateException("Activity must implement EmployeeDataHolder interface");
  }

  private void yourEmployeeDataProcessingMethod(){
    EmployeeData employeeData = mEmployeeDataHolder.getEmployeeData();
    // process data, populate views etc.
  }

  @Override
  public void onDetach() {
    super.onDetach();
    mEmployeeDataHolder = null;
  }
}

这篇关于如何留住片段之间的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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