user2145673
user2145673

Reputation: 363

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 (onDestroy?, onDetach?) 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 setCurrentitem() method.

https://github.com/romannurik/Android-WizardPager https://github.com/Nimrodda/WizarDroid

Appreciate your assistance

Upvotes: 0

Views: 716

Answers (1)

Iatsenko Anton
Iatsenko Anton

Reputation: 157

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 {
        @Override
        public void onPauseFragment() {
            //save your data here
        }}

    public class NewHomeActivity extends Activity {
        @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //.........
        //..........
 final ViewPager.SimpleOnPageChangeListener listener = pageChangeListener(YourPagerAdapter);
        YourViewPager.addOnPageChangeListener(listener);


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

                int currentPosition = 0;

                @Override
                public void onPageSelected(int newPosition) {

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

                    currentPosition = newPosition;
                }
            };
        }}

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

Upvotes: 1

Related Questions