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!
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 ).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With