As topic states I’m trying to implement swipable tabs using Fragments and ActionBar. I’ve iplemented it successfully using ActionBarSherlock and code from examples with the TabsAdapter class (subclass to FragmentPageAdapter). Some of the fragments should be able to show a new tab in the same tab spor, and this is where the problem arises. I really can’t figure out how to replace a fragment in the pager with a new one. I’ve googled and tried out a lot of stuff.
I can replacie the fragment in the internal collection holding the fragments classes inside the FragmentPageAdapter, thus making the new Fragment available to the getItem method but that doesn’t do it as I can’t figure out how to update the pager to show the new Fragment. Even after switching to another Tab and going back I still get the old Fragment which I suppose is being cached somewhere by the pager. So is there a way to notify an update to the pager?
Maybe I should just forget using FragmentPageAdapter ? In that case what is the right way to get all this functionallity happen?
P.S. If you want to see my code let me know, but it’s basicly the same as in the examples and the problem is more general I think
When you use a
ViewPagerit keeps the visible page/fragment in memory in addition to pages/fragments that you can navigate to. In addition to this, if theViewPageris using aFragmentPagerApdapterit keeps the views in memory for the life of the activity.I answered a similar post to yours above removing a fragment which can be found here. Your case is an extension of this, removing and then adding something else.
https://stackoverflow.com/a/10399127/629555
Basically, you need to use a
FragmentStatePagerAdapter, override thegetItemPositionmethod to returnPagerAdapter.POSITION_NONEand make sure you call.notifyDataSetChanged();on your adapter when you want it changed.The answer to the link above covers this in much more detail and provides a code example.