1
votes

I'm working on an App utilizing ActionBarSherlock with navigation tabs and multiple fragments.

I have three navigation tabs each with one or more child views. I also have three option menu items with one or more child views. Everything works fine when I drill down the views within each Tab or option menu. Each initial subview is a ListFragment that calls a detail view or views.

I run into problems when I start switching tabs while in the child views or the option menu fragment. My views start to overlap with the previous view underneath the new view.

Here is my 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
}

Here is my option menu:

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

I have one MainActivity for all of the fragments. How do you handle the backstack when dealing with drill down child views and ActionBar navigation tabs?? Any help or suggestions would be greatly appreciated!

1
I am interested too. Have you worked it out?Noodles

1 Answers

1
votes

Here is the solution that worked for me:

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