user2145673 user2145673 - 1 year ago 66
Android Question

keep data persistent between fragments

I am trying to build a wizard with the help of the ViewPager, in each step the user will input that data and at the end all data inserted should be presented to the user before final confirmation and send all data to the server.

What i would like to know are the following:

  1. how can I keep data persistent so at the final fragment everything will be presented to the user? I would like to use a method that will be the best in terms of performance and efficiency.

  2. On which event (
    ?) should the data be saved when the user press the "Next" button to move the next step in the wizard?

keep in mind that on each step more data should be kept.

I saw solution for wizards for android however my issue with them is that they are not RTL supported so with the ViewPager I can set the initial step using

Appreciate your assistance

Answer Source

I think it is more yours architecture design decision.

For example all your Fragments classes should implement custom interface vs onPause() method. For what, you sinking, main Fragment.class have one onPause()? It because when you load fragments in view pager adapter, he present only one but really he load 3 or more fragments from fragmentsArray and cache them, and when you only on first fragment onPause() has worked on second and third fragment and so on.... at StackOverflow was decision for this problem like :

 public interface FragmentLifeCycle {
    void onPauseFragment(); } ///for saving your data you must implement it in all fragments from view Pager 

     public class SomeFragment extends Fragment implements FragmentLifeCycle {
        public void onPauseFragment() {
            //save your data here

    public class NewHomeActivity extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
 final ViewPager.SimpleOnPageChangeListener listener = pageChangeListener(YourPagerAdapter);

     private ViewPager.SimpleOnPageChangeListener pageChangeListener(final FragmentPagerAdapter adapter) {
            return new ViewPager.SimpleOnPageChangeListener() {

                int currentPosition = 0;

                public void onPageSelected(int newPosition) {

                    FragmentLifeCycle fragmentToHide = (FragmentLifeCycle) adapter.getItem(currentPosition);

                    currentPosition = newPosition;

But its only one decision , i see at this problem decorator or state machine design pattern.