Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionBar List Navigation Overlapping Fragments

I'm trying to implement the Android Action Bar in list navigation mode, it successfully changes fragments when an item is selected from the list, but the fragments overlap and I can see the content of the previous one still on the screen when the second is selected. Here's my code for the Activity's OnCreate and OnNavigationItemSelected:

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    mFirstFragment = new FirstFragment();
    mSecondFragment = new SecondFragment();

    SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(
            this, 
            R.array.action_list, 
            android.R.layout.simple_spinner_dropdown_item);

    mActionBar = getActionBar();
    mActionBar.setDisplayShowTitleEnabled(false);
    mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    mActionBar.setListNavigationCallbacks(mSpinnerAdapter, this);

    if(savedInstanceState != null) {
        mActionBar.setSelectedNavigationItem(
                savedInstanceState.getInt("currFragment"));
    }
}

public boolean onNavigationItemSelected(int position, long itemId) {
    FragmentTransaction mFragmentTransaction = 
            getFragmentManager().beginTransaction();

    switch(position) {
        case FIRST_FRAGMENT:
            mFragmentTransaction.replace(
                    android.R.id.content, 
                    mFirstFragment);
            break;
        case SECOND_FRAGMENT:
            mFragmentTransaction.replace(
                    android.R.id.content, 
                    mSecondFragment);
            break;
    }
    mFragmentTransaction.commit();

    return true;
}

Thanks in advance!

like image 763
user1387981 Avatar asked May 10 '12 19:05

user1387981


1 Answers

I had this same problem. The accepted answer in FragmentTransaction .attach and .detach for Actionbar tabs worked for me. You may also get good pointers from Android Action Bar Tab with scrollview made duplicate view after orientation change ( although the key insights that worked for me came from the first question that I have linked to ).

like image 103
Ngure Nyaga Avatar answered Oct 23 '22 19:10

Ngure Nyaga