1

我正在开发一个使用带有导航选项卡和多个片段的 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?任何帮助或建议将不胜感激!

4

1 回答 1

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;
}
于 2012-08-23T13:59:19.870 回答