FragmentActivity和Fragments:popBackStack

时间:2011-09-25 16:56:45

标签: android android-fragments

My FragmentActivity在tabhost中管理4个listfragments(对于每个listfragment,我都会记录它的backstack)。 ListFragment共享一个FrameLayout,在其中附加其内容。触发onListItemClick时的每个ListFragment让FragmentActivity启动一个新的Fragment,以便用新的片段替换当前片段的内容。 如果您调用A当前显示的Fragmetn(由ListFragment A管理)和B,则在内容A与B的内容重叠的片段之间切换时,会发生将替换A(由ListFragment B管理)的片段,至少是i清除片段的后台关闭(示例中为A)。按片段之间的顺序我做

    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);
            }
        }
    }

当我在触发OnListemItemClick时需要更改listfragment内容时,我使用

    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();
}
你可以帮我理解为什么吗?提前谢谢,抱歉我的英文不好

编辑:我的问题是为什么每次在ListFragment之间切换时都需要清除backstack,以避免Fragment的内容重叠。我错了什么

1 个答案:

答案 0 :(得分:5)

好的,所以这个答案假设你想在每次交换标签时擦除每个标签的历史记录。我的意思是Tab 1从frag 1开始,然后你点击并将其更改为frag 2.如果你选择Tab 2,你将撤消Tab 1的历史记录,下次你点击Tab 1你会回到frag 1。

说到这里是解决方案:用下面的

替换你的onTabUnselected
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);
        }
    }