Android:Fragment堆栈与ActionBar和Tabs的混淆

时间:2012-07-20 22:40:25

标签: android android-fragments android-actionbar actionbarsherlock

我正在使用带有导航标签和多个片段的ActionBarSherlock的应用程序。

我有三个导航标签,每个标签都有一个或多个子视图。我还有三个带有一个或多个子视图的选项菜单项。当我在每个选项卡或选项菜单中向下钻取视图时,一切正常。每个初始子视图都是一个ListFragment,用于调用详细视图。

当我在子视图或选项菜单片段中开始切换标签时遇到问题。我的观点开始与新视图下方的上一个视图重叠。

这是我的TabListener:

class MyTabsListener implements ActionBar.TabListener {
public SherlockListFragment fragment;

public MyTabsListener(SherlockListFragment fragment) {
    this.fragment = fragment;
}

public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
    ft.replace(android.R.id.content, fragment);
}

public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {      
    ft.remove(fragment);
}

public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
    // Don't care if a tab is reselected, no special handling required
}

这是我的选项菜单:

   @Override
public boolean onOptionsItemSelected(MenuItem item) {

    // initialize variables
    FragmentManager fm = null;
    FragmentTransaction ft = null;
    ActionBar actionBar = getSupportActionBar();

    // check to see which action item was selected
    switch (item.getItemId()) {
    case android.R.id.home:
        // initialize variables
        fm = this.getSupportFragmentManager();
        ft = fm.beginTransaction();

        if (fm.getBackStackEntryCount() > 0) {

            fm.popBackStack();
            ft.commit();    
        }else {
            actionBar = getSupportActionBar();
            actionBar.setDisplayHomeAsUpEnabled(false);
        }   
        return true;
    case R.id.menuitem_search:
        onSearchRequested();
        return true;
    case R.id.menuitem_contacts:
        ContactListFragment contactListFragment = new ContactListFragment();    
        ft = getSupportFragmentManager().beginTransaction();
        ft.replace(android.R.id.content, contactListFragment);      
        ft.addToBackStack(null);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 
        ft.commit();
        return true;
    case R.id.menuitem_isolation:
        ISOListFragment isoListFragment = new ISOListFragment();    
        ft = getSupportFragmentManager().beginTransaction();
        ft.replace(android.R.id.content, isoListFragment);      
        ft.addToBackStack(null);           
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 
        ft.commit();
        return true;
    case R.id.menuitem_water:
        H2OListFragment h2oListFragment = new H2OListFragment();    
        ft = getSupportFragmentManager().beginTransaction();
        ft.replace(android.R.id.content, h2oListFragment);      
        ft.addToBackStack(null);          
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 
        ft.commit();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

我有一个MainActivity用于所有片段。在处理向下钻取子视图和ActionBar导航选项卡时,如何处理backstack?任何帮助或建议将不胜感激!

1 个答案:

答案 0 :(得分:1)

以下是适合我的解决方案:

@Override
public void onBackPressed() {

    // initialize variables
    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();

    // check to see if stack is empty
    if (fm.getBackStackEntryCount() > 0) {          
        fm.popBackStack();
        ft.commit();    
    }
    else {

        super.onBackPressed();
    }
    return;
}