We are trying to create an Android application that is based on ViewPager that receive during runtime instructions to add and remove pages. Almost like a tabbed browser experience, that you can remove the current tab, or remove a specific tab.
Following Google documentation we use FragmentStatePagerAdapter, that is intended to be used in situations when there are a large number of pages, working more like a list view.
But when we try to: - Remove a page that is not on the screen - and create a new fragment object from the same class - and on the same position of the page removed We noticed that the Android platform recover the dead page and display that to the user. The new object that we just created simply does not run “onCreate”, “onCreateView” or “onActivityCreated”.
We are looking to ways to fix this issue forcing the platform to use our new fragment object from the same class. Any ideas?
We found out that if we destroy the current page the platform indeed destroyed the page and created a new object from the same class. Here is a small example replicating the problem and this behavior.
Source: http://dl.dropbox.com/u/8333012/SimpleAdapter/SimplePager.zip
Video: http://www.youtube.com/watch?v=-oaXXwU8PSI&hd=1
From this project when you touch the TextView on the first page it was designed to remove the second page (which is green) to a new blue page. You will see that even doing that from the first page the second page remains green. But when you press the back Android button on the second page (green) and touch the TextView the second page created will be the right blue color.
When you are dealing with ListView's and change the underlying data of your adapter you call notifyDataSetChanged()
and any View reflecting the data set will refresh itself. Thats the way you should go with fragment pager adapter too.
In your case you do not notify the adapter. However, in case of FragmentPagerAdapter
/FragmentStatePagerAdapter
it does not make a difference because those adapters ignore notifyDataSetChanged()
"by default". To make it work override the getItemPosition()
in your adapter implementation
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
And as already said, after you add/remove fragments call (in your showOtherPage()
)
mAdapter.notifyDataSetChanged();
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