I have 5 fragments in ViewPager
used to fill business object with several fields step by step, in each step some of those fields will be set. I\'ve read many articl
I wouldn't recommend a global singleton. There are two main reasons:
onSaveInstanceState()
, and restoring it in onCreate()
.All of the Fragments in the ViewPager can get a reference to the parent Activity via a call to getActivity()
. Or if your ViewPager is within a Fragment, then all of the Fragments can access the parent Fragment via a call to getParentFragment()
. You can then cast the result to the appropriate class (or better yet, interface) and make method calls to pass data back and forth. Keep track of your business data in the parent Activity/Fragment. This way, you don't need a global singleton
For example,
public class MyParentFragment extends Fragment {
private String mPageOneData;
private int mPageTwoData;
private List mPageThreeData;
public void setPageOneData(String data) {
mPageOneData = data;
}
...
}
public class PageOneFragment extends Fragment {
private void sendDataToParent(String data) {
Fragment f = getParentFragment();
if (f != null && f instanceof MyParentFragment) {
MyParentFragment parent = (MyParentFragment) f;
f.setPageOneData(data);
}
}
}