I'm using a Viewpager
with a the FragmentPagerAdapter
to allow adding and removing of pages. Each page displays data obtained from the internet.
As a new page is added, a new Fragment is associated with that page. The data are obtained via AsyncTask and displayed in the Fragment. When the user chooses to remove a page, the idea is to destroy the page and the associated Fragment.
In general, this all works well. The problem I'm seeing is as follows:
You have three pages with data:
[Page 1] [Page 2] [Page 3]
You delete any page other than the last one, say page 2; Page 2 disappears as desired:
[Page 1] [Page 3]
You add a new page; but instead of a blank, new page, the new page shows the data (view) from page 3.
[Page 1] [Page 3] [Page 4, but showing view/data of Page 3, should be blank]
The page removal code in my activity is as follows:
// Destroy fragment for this page
DataListFragment curFrag = getFragmentByName(currentPage);
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().remove(curFrag).commit();
fm.executePendingTransactions();
curFrag = null;
// Remove page and update adapter
mPageTitles.remove(position);
mAdapter.notifyDataSetChanged();
Using the debugger, it shows that the fragment is removed from the FragmentManager
after the executePendingTransactions()
call. But in the FrampePagerAdapters
call, mAdapter.notifyDataSetChanged()
, the fragment is added back in and then displayed when a new page is created.
I tried using the FrameStatePagerAdapter, since that should allow destroying fragments, but it did not work. In my FragmentPagerAdapter's getItemPosition()
method, I use return FragmentAdapter.POSITION_NONE;
as pointed out in another SO article I came across.
It seems as if the View for that page is not destroyed, but then added back into the new page. I tried using the removeViewAt()
method on the view of the new page, but that did not work.
Being new to this, I'm sure I'm missing something obvious...
This method may be called by the ViewPager to obtain a title string to describe the specified page. This method is deprecated.
In ViewPager2 you do not need to use Fragments at all. Just create an Adapter as you would a RecyclerView Adapter. and set it to ViewPager2. One thing to keep in mind however is layout height of the view you want to inflate should be match_parent and not wrap_content as you would for a recyclerView.
The getItemPosition() method tells the ViewPager whether or not to delete viewItem, to redraw or not redraw viewItem when the adapter. notifyDataSetChanged() is called. Basically your implementation of getItemPosition() method is called to answer a simple question when adapter. notifyDataSetChanged() is called.
The difference is that you can use Fragments inside a FragmentPageAdapter . If you want to have fragments that are going to be used in other parts of your code this is your Adapter. Otherwise if you only want to have code that isn't going to be reused, you can use PagerAdapter .
You should rather extend FragmentStatePagerAdapter
and remove corresponding item from the list of items in that adapter, instead of trying to remove a Fragment from activity. Do not forget to call adapter.notifyDataSetChanged()
after you removed an item in adapter. Once done, ViewPager
and FragmentStatePagerAdapter
will take care for the rest.
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