0

Here, I have an Activity with viewpager which has FragmentA , FragmentB, FragmentC and FragmentD in the adapter.

All Fragments has FORM data. When I swipe from FragmentA to FragmentB, I have to save the form data of FragmentA before FragmentB is visible and in the same way when swipe from FragmentB to FragmentC I have to save the form data of FragmentB before FragmentC is visible.

Is this possible? If yes, How to do this?

jazzbpn
  • 6,441
  • 16
  • 63
  • 99
  • use setUserVisibleHint() method to manage your Fragment check or check this http://stackoverflow.com/questions/24161160/setuservisiblehint-called-before-oncreateview-in-fragment – Farmer Nov 22 '16 at 15:40
  • Yup, this works. Thanks !! – jazzbpn Nov 23 '16 at 08:28

1 Answers1

0

You can save those details in Shared preferences while you swiping to next fragment. For swiping

            viewPagerBanner.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            private static final float thresholdOffset = 0.5f;
            private boolean scrollStarted, checkDirection, check;

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if (checkDirection) {
                    if (thresholdOffset > positionOffset && position == 0) {
                        check = true;
                    } else {
                        check = false;
                    }
                    checkDirection = false;
                }
            }

            @Override
            public void onPageSelected(int position) {
                currentPosition = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

                // You can save current form fields here
                }

            }
        });

Shared Preferences

SharedPreferences sharedHead = context.getSharedPreferences("UniqueName", Context.MODE_PRIVATE);    
SharedPreferences.Editor editor = sharedHead.edit();
editor.putInt("name", "Developer");
editor.apply();

To read those data Last fragment

String name = sharedHead.getInt("name", "noName");

I hope this may help you. Thanks

Srihari
  • 2,387
  • 9
  • 51
  • 82