Android Fragment的子片段:从后台重新打开时无法显示其他片段

时间:2014-08-04 07:55:12

标签: android android-fragments

MainActivity有4个片段,第一个片段也有一些子片段。问题是:当应用程序转换背景一段时间后,重新打开它,然后MainActivity只能显示第一个片段及其子片段,其他3个片段无法显示。

private void setTabIndex(int index) {

if (nowTabIndex == index) {
    return;
}

FragmentTransaction fragmentTransaction = getSupportFragmentManager()
        .beginTransaction();

unSelectAllTab();

switch (index) {
case 0: 
    if(!infoFragment.isAdded()) 
    {
        fragmentTransaction.add(R.id.main_content, infoFragment,
                FRAGMENT_INDEX_INFO); 
    }
    else
    {

    }

    if (currentFragment != null) {
        fragmentTransaction.hide(currentFragment);
        //  fragmentTransaction.setVisibility(View.GONE);
    } 
    fragmentTransaction.show(infoFragment); 

    currentFragment = infoFragment;
    infoTabLayout.setSelected(true);

    break;
case 1: 
    if(!marketFragment.isAdded())
    {
        fragmentTransaction.add(R.id.main_content, marketFragment,
                FRAGMENT_INDEX_MARKET);
    } 

    if (currentFragment != null) {
        fragmentTransaction.hide(currentFragment);
    }

    fragmentTransaction.show(marketFragment);
    currentFragment = marketFragment;

    marketTabLayout.setSelected(true);
    break;
default:

    fragmentTransaction.replace(R.id.main_content, infoFragment);
    //fragmentTransaction.addToBackStack(null);
    break;
}

fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
nowTabIndex = index;

}

在上面的代码中,当从背景中重新打开它一段时间(应用程序已在后台被杀死)时,第一个片段可以显示,第二个片段无法显示。怎么解决?

0 个答案:

没有答案