Remove or Clear All Fragments From ViewPager
Asked Answered
A

5

7

I'm populating view pager through fragments likes this

pagerAdapter = new PagerAdapter(getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.container);

        for (int i = 0; i < 10; i++) {
            newfragment fragobj = new newfragment();
            fragobj.setArguments(bundle);
            pagerAdapter.addFragment(fragobj, "Category");
        }

            mViewPager.setAdapter(pagerAdapter);
            mViewPager.setCurrentItem(page);

I want to clear all fragments and add new fragments after clearing. If I add new fragments now it ads on with old fragments.

I tried many stack overflow answers but none of them worked for me.

Ami answered 27/10, 2016 at 19:50 Comment(0)
S
21

the fragment was already getting removed and destroyed. the issue was of viewpages.

Only this worked for me

myViewPager.setSaveFromParentEnabled(false);

Cheers !

Sedan answered 18/9, 2017 at 6:11 Comment(0)
I
8

Add this method in ViewPagerAdapter and call it when you want to clear fragments from viewPager

public void clear() {
    FragmentTransaction transaction = manager.beginTransaction();
    for (Fragment fragment : mFragmentList) {
        transaction.remove(fragment);
    }
    mFragmentList.clear();
    transaction.commitAllowingStateLoss();
}
Izaguirre answered 17/11, 2017 at 14:43 Comment(1)
This is exactly what i was looking for. View adapter view were not removing once remove items and call notifyDataSetChanged method. Thank you so much.Bowfin
M
2

I use navController fragment and in one fragment I also had viewPager with few slides set in a "parent fragment, which was part of the nav tree".

Sadly after the parent fragment was destoryed, the frags created within it for view pager were not getting cleared and they were restored while resuming the app, even tho they couldn't be shown!!

I've set adapter to null in parent's fragment onDestroy()-. and it clears all frags used inside viewPager. enter image description here enter image description here

If you have similiar problem, this wokrs 100%.

Multipurpose answered 2/10, 2022 at 22:2 Comment(0)
B
1

mViewPager : is the view you are using to set you Fragment

mViewPager = (YourViewPager) findViewById(R.id.myPager);

TABLE : is just a Integer list of the position of all my Fragments

public void destroyAllItem() {
        int mPosition = mViewPager.getCurrentItem();
        int mPositionMax = mViewPager.getCurrentItem()+1;
        if (TABLE.size() > 0 && mPosition < TABLE.size()) {
            if (mPosition > 0) {
                mPosition--;
            }

            for (int i = mPosition; i < mPositionMax; i++) {
                try {
                    Object objectobject = this.instantiateItem(mViewPager, TABLE.get(i).intValue());
                    if (objectobject != null)
                        destroyItem(mViewPager, TABLE.get(i).intValue(), objectobject);
                } catch (Exception e) {
                    Log.i(TAG, "no more Fragment in FragmentPagerAdapter");
                }
            }
        }
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        super.destroyItem(container, position, object);

        if (position <= getCount()) {
            FragmentManager manager = ((Fragment) object).getFragmentManager();
            FragmentTransaction trans = manager.beginTransaction();
            trans.remove((Fragment) object);
            trans.commit();
        }
    }
Beta answered 28/10, 2016 at 1:11 Comment(2)
I'm not an expert so can you please tell me more about that TABLE variable and how can I create it.Ami
TABLE.size() is the number of fragment in viewpager. You can get the number of fragment from your viewpager adapterBeta
G
1

With ViewPager2 (1.0.0) and FragmentStateAdapter I also found the current view not to be recreated after updating the data set using notifyDataSetChanged, even when the data corresponding to the current view is changed. Apparently, notifyDataSetChanged doesn't perform a very thorough investigation of what has been changed and assumes the current view to be still correct.

The workaround I found was to simply set the same adapter that I had already set for the previous data set again:

viewPager2.setAdapter(myFragmentStateAdapter);

Apparently, setting an adapter makes it flush the cached views, which makes very good sense of course. Luckily, it doesn't check whether it's the same adapter.

Gerhardine answered 14/7, 2020 at 19:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.