jazzbpn
jazzbpn

Reputation: 7318

How to save ViewPager FORM data onSwipe to next pager android

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?

Upvotes: 0

Views: 781

Answers (1)

Srihari
Srihari

Reputation: 2429

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

Upvotes: 0

Related Questions