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.
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.
ViewPager save Fragment in FragmentManager with particular tags, So We can get ViewPager's fragment by FragmentManager class by providing tag. And ViewPager provide tag to each fragment by following syntax : Fragment page = getSupportFragmentManager(). findFragmentByTag("android:switcher:" + R.
This method may be called by the ViewPager to obtain a title string to describe the specified page. This method is deprecated. This method should be called by the application if the data backing this adapter has changed and associated views should update.
ViewPager2 is an improved version of the ViewPager library that offers enhanced functionality and addresses common difficulties with using ViewPager . If your app already uses ViewPager , read this page to learn more about migrating to ViewPager2 .
the fragment was already getting removed and destroyed. the issue was of viewpages.
Only this worked for me
myViewPager.setSaveFromParentEnabled(false);
Cheers !
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With