I am using a ViewPager in conjunction with ActionBar tabs, as illustrated here. I'm using ActionBarSherlock for backward compatibility, so the parent activity extends SherlockFragmentActivity, and the children fragments extend SherlockFragment.
The solution works great for tabs with swiping, but now I want to dynamically change a fragment associated with one of the tabs.
I've read through numerous S.O. answers on this subject (for example here and here), but I've not found a clear explanation of how to dynamically change a fragment when using ViewPager + the TabsAdapter above.
Here's what I have now. When the user hits a button on an existing fragment, I try to replace the fragment from the parent activity as follows:
AnotherFragment fragment = new AnotherFragment();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
int viewId = R.id.pager; // resource id of the ViewPager
int position = Constants.TAB_2; // tab pos of the fragment to be changed
String tag = "android:switcher:" + viewId + ":" + position;
ft.replace(viewId, fragment, tag);
ft.commit();
mTabsAdapter.notifyDataSetChanged();
This doesn't work, so I'm missing something. I also tried doing it with nested fragments using getChildFragmentManager(), but ran into a problem since this function isn't available without API 17, Android 4.2.
Thanks for any help!