0

I am confused with FragmentTabHost to do fragment replacement. I have a few pages that have some views to click and then forward to another fragment, but after I replace that fragment and then click on other tabs, the fragments overlap together...

here is my switching function:

public void fragmentSwitch(FragmentActivity context, Fragment fragment, String fragmentTag){
    FragmentManager fragmentManager = context.getSupportFragmentManager();
    fragmentManager.beginTransaction()
            //.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right)
            .replace(android.R.id.tabcontent, fragment, fragmentTag)
            //.addToBackStack(null)
            .commit();
    fragmentManager.executePendingTransactions();
}
Simon
  • 481
  • 2
  • 9
  • 20
  • I don't think FragmentTabHost supports replacing the fragments on the tabs. The docs don't mention that, and in the source it looks like the TabInfo object it uses for each tab is caching the fragment instance. If you go straight to the FragmentManager and replace the fragment, nobody is going to tell the FragmentTabHost about it, so it will still have the old fragment. – Bruce Dec 01 '14 at 08:27
  • How can I do with it?? correct the cache? – Simon Dec 01 '14 at 09:18
  • I looked for an API before my last comment, where you could tell the tab about the new fragment, but I didn't see one. The two options that come to mind are 1) don't replace the fragment, but just configure the current one (not sure if this is possible for you), or 2) use FragmentPagerAdapter along with action bar navigation tabs. This is the approach Google is pushing, and with the support library it works on old devices too. I can give you more guidance on either of these, if you tell me which one sounds better for you. – Bruce Dec 01 '14 at 19:28
  • @Bruce I think I have to search for how to use the viewpager :) Thanks so much – Simon Dec 03 '14 at 04:36

0 Answers0