user1387981
user1387981

Reputation: 133

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!

Upvotes: 7

Views: 3585

Answers (1)

Ngure Nyaga
Ngure Nyaga

Reputation: 3027

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 ).

Upvotes: 1

Related Questions