Remove or Clear All Fragments From ViewPager

Jack Stern picture Jack Stern · Oct 27, 2016 · Viewed 12.9k times · Source

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.

Answer

Kathan Shah picture Kathan Shah · Sep 18, 2017

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

Only this worked for me

myViewPager.setSaveFromParentEnabled(false);

Cheers !