Android – NavigationDrawer menuItem setChecked doesnt work

androidmenuitemnavigation-drawer

i created a BaseActivity with a NavigationDrawer from the Android support design library.

public abstract class BaseActivity extends ActionBarActivity {  

final static int MENUITEMS = 5;

private Toolbar toolbar;                              // Declaring the Toolbar Object

NavigationView navigationView;
private DrawerLayout drawerLayout;

Context basecontext;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(getLayoutId());

/* Assinging the toolbar object ot the view
and setting the the Action bar to our toolbar
 */
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    //Initializing NavigationView

    //Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

        // This method will trigger on item Click of navigation menu
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {


            //Checking if the item is in checked state or not, if not make it in checked state
            if (menuItem.isChecked()) {
                menuItem.setChecked(false);

            } else {
                menuItem.setChecked(true);
                switch (menuItem.getItemId()) {


                    //Replacing the main content with ContentFragment Which is our Inbox View;
                    case R.id.menu_start:
                        Toast.makeText(getApplicationContext(), "Noch nicht implementiert", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(BaseActivity.this, Start.class);
                        startActivity(intent);
                        // For rest of the options we just show a toast on click
                        //TODO

                    case R.id.menu_all_entries:
                        Toast.makeText(getApplicationContext(), "Noch nicht implementiert", Toast.LENGTH_SHORT).show();
                    case R.id.menu_missed_entries:
                        Toast.makeText(getApplicationContext(), "Noch nicht implementiert", Toast.LENGTH_SHORT).show();
                    case R.id.menu_diagram:
                        Toast.makeText(getApplicationContext(), "Noch nicht implementiert", Toast.LENGTH_SHORT).show();
                    case R.id.menu_export:
                        Toast.makeText(getApplicationContext(), "Noch nicht implementiert", Toast.LENGTH_SHORT).show();
                    default:
                        Toast.makeText(getApplicationContext(), "Somethings Wrong", Toast.LENGTH_SHORT).show();

                }
            }

            //Closing drawer on item click
            drawerLayout.closeDrawers();
            //String localClassName = getLocalClassName();
            //Check to see which item was being clicked and perform appropriate action
            return true;

        }
    });

This works just fine.

Now i have a "Sub"-Activity (nothing that is represented as a menu item).

In this Activity:

public class AddEntryPage1 extends BaseActivity {


@Override
protected int getLayoutId(){
    return R.layout.activity_add_entry_page1;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ButterKnife.bind(this);
    for(int i=0;i<MENUITEMS;i++){
        navigationView.getMenu().getItem(0).setChecked(false);
    }
}
}

i tried to set all my menuitems to unchecked, but nothing happens.

When i evaluate the expression

navigationView.getMenu().getItem(0).isChecked()

it is always true (before and after the
for-loop/getItem(0).setChecked(false))
.

I also get no error on this.

Seems like i have no writing access?
Is it a problem that i have initiated my drawer.xml with a selected item? When i click an other menu item the selected item changes as expected.
Or does checkableBehavior="single" means, that one item must be checked?

<group android:checkableBehavior="single">

    <item
        android:id="@+id/menu_start"
        android:checked="true"
        android:icon="@drawable/ic_action_home"
        android:title="Start" />

    <item
        android:id="@+id/menu_all_entries"
        android:checked="false"
        android:icon="@drawable/ic_action_view_list"
        android:title="Alle Eintr├Ąge" />

    <item
        android:id="@+id/menu_missed_entries"
        android:checked="false"
        android:icon="@drawable/ic_action_view_list"
        android:title="Verpasste Eintr├Ąge" />

    <item
        android:id="@+id/menu_diagram"
        android:checked="false"
        android:icon="@drawable/ic_editor_insert_chart"
        android:title="Diagramm" />


    <item
        android:id="@+id/menu_export"
        android:checked="false"
        android:icon="@drawable/ic_social_share"
        android:title="Export" />

</group>

Thanks for helping me out!

Best Solution

For anyone who might have the same issue:

I reported the issue to Google: https://code.google.com/p/android/issues/detail?id=184089

Related Question