0

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

Community
  • 1
  • 1
Aditi K
  • 1,534
  • 5
  • 22
  • 43

3 Answers3

3

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;
  }
}
Bartek Filipowicz
  • 1,235
  • 7
  • 9
  • @AditiK I've edited the answer to give you a sample of activity as the data holder – Bartek Filipowicz Jan 24 '14 at 14:00
  • Is It good solution for my Case? using bundle [Link] http://stackoverflow.com/questions/20036548/pass-data-between-fragments – Aditi K Jan 27 '14 at 04:57
  • Yes, it will work. The difference is that it won't be the same Employee object in all the fragments, but rather a separate copy in each of them. Another downside of using fragment arguments is that once you set it, you cannot modify it. But in the end this should work in your case. – Bartek Filipowicz Jan 27 '14 at 09:27
  • 1
    Should the reference to the activity not be a [WeakReference](https://docs.oracle.com/javase/7/docs/api/java/lang/ref/WeakReference.html)? – Rule Jan 08 '16 at 09:16
0

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.

cYrixmorten
  • 7,110
  • 3
  • 25
  • 33
0

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();
Muhammad Aamir Ali
  • 20,419
  • 10
  • 66
  • 57