Aditi K
Aditi K

Reputation: 1554

how to retain data between Fragments

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

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

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

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

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

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

is shared preferences best solution for it? Is It good solution for my Case? using bundle

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

Upvotes: 0

Views: 2545

Answers (3)

Bartek Filipowicz
Bartek Filipowicz

Reputation: 1235

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 you should store the common object in the Activity that hosts all your fragments, and save/restore it via onSaveInstanceState/onRestoreInstanceState mechanics.

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.

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

Upvotes: 3

Muhammad Aamir Ali
Muhammad Aamir Ali

Reputation: 21087

You should change your fragments navigation through this mViewPager.setCurrentItem(currentPage, true); Throught this data fields will be retained automatically.

If you want to access all fragment data and fields you can try this.

YourFragment fragment = new YourFragment();
fragment.editText.getText().toString();

Upvotes: 0

cYrixmorten
cYrixmorten

Reputation: 7108

Shared preferences is definitely an option.

Even simpler, you could make the fields of Employee holder class static. This way the values set to the fields will remain as your incrementally input data through the fragments.

Upvotes: 0

Related Questions