FragmentActivity and Fragments: popBackStack
My FragmentActivity manages 4 listfragments (for every listfragment I keep trace of its backstack ) within tabhost. The ListFragment shares a FrameLayout in which they attach their content. Every ListFragment when onListItemClick is fired let's the FragmentActivity start a new Fragment so that the content of the current fragment is replaced with the new fragment. If you call A the fragmetn currently showing (managed by ListFragment A) and B the fragment that would replace A (managed for instance by ListFragment B) happens when switching between fragment that the content A overlaps with the content of B, at least that i clear the backstack of the fragment switched off (A in the example). In order between fragment I do
if (activeTab != tv) {
if (activeTab != null) {
Log.i(TAG, "tag: " + activeTab.getTag() + " detaching...");
FragmentInfo fragmentInfo = fragments.get(activeTab.getTag());
//detach the current fragment
//getSupportFragmentManager().popBackStack((String)activeTab.getTag(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
ft.detach(fragmentInfo.fragment);
}
//get the new
FragmentInfo fragmentInfo = fragments.get(tv.getTag());
Log.i(TAG, "tag: " + tv.getTag() + " fragment: " + fragmentInfo.mClass.getName());
if (fragmentInfo != null) {
if (fragmentInfo.fragment == null) {
fragmentInfo.fragment = Fragment.instantiate(this, fragmentInfo.mClass.getName(), fragmentInfo._args);
ft.add(R.id.mytabcontent, fragmentInfo.fragment, fragmentInfo._tag);
} else {
Log.i(TAG, "attacching fragment: " + fragmentInfo.mClass.getName());
ft.attach(fragmentInfo.fragment);
}
}
}
while when I need to change the listfragment content when OnListemItemClick is fired I use
private void replaceFragment(Fragment fragment, String tag, String backstack) {
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.mytabcontent, fragment, tag);
ft.setTransition(FragmentTransaction.TRANSIT_NONE);
ft.addToBackStack(backstack);
ft.commit();
}
Could you please help me understand why? Thanks in advance and sorry for my bad english
EDIT: my question is why I need to clear the backstack eve开发者_开发知识库rytime I switch between ListFragment in order to avoid that content of the Fragment overlaps. What I am making wrong
Ok, so this answer assumes you want to wipe each tabs back history every time you swap tabs. What I mean by that is Tab 1 starts on frag 1, then you click and change it to frag 2. If you select Tab 2, you will be undoing the history of Tab 1 and next time you click Tab 1 you will be back to frag 1.
With that said here is the solution: Replace your onTabUnselected with the below
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
//this segment removes the back history of everything in the tab you are leaving so when you click on the tab again you go back to a fresh start
FragmentManager man = mActivity.getFragmentManager();
if(man.getBackStackEntryCount()>0) //this check is required to prevent null point exceptions when clicking off of a tab with no history
man.popBackStack(man.getBackStackEntryAt(0).getName(), FragmentManager.POP_BACK_STACK_INCLUSIVE); //this pops the stack back to index 0 so you can then detach and then later attach your initial fragment
//also it should be noted that if you do popbackstackimmediate here instead of just popbackstack you will see a flash as the gui changes back to the first fragment when the code executes
//end
ft.detach(mFragment);
}
}
精彩评论