Search code examples
androidnavigationview

navigation drawer wrong checked when i change activity


I have a navigation drawer in my application with different activities. Everything goes well, but I have a big problem. When I want to change activity in navigation drawer, the item isn't checked or checked wrong. For example, I want to go in Activity B, when I open navigation drawer isn't selected or is selected another item. Here there are some codes: items:
`

<item
    android:id="@+id/nav_home"
    android:icon="@drawable/ic_home"
    android:title="@string/Button_home"/>

<group android:checkableBehavior="single"
    android:id="@+id/anonimous_group">

    <item
        android:id="@+id/nav_registrati"
        android:icon="@mipmap/ic_create_black_24dp"
        android:title="@string/registrati"/>
    <item
        android:id="@+id/nav_login"
        android:icon="@mipmap/ic_filter_tilt_shift_black_24dp"
        android:title="@string/accedi"/>
</group>

<group android:checkableBehavior="single"
    android:id="@+id/logged_group"
    >
    <item
        android:id="@+id/nav_profilo"
        android:icon="@drawable/id_profile"
        android:title="@string/Profilo"/>
    <item
        android:id="@+id/nav_notifiche"
        android:icon="@drawable/ic_notifications"
        android:title="@string/Notifiche"/>
    <item
        android:id="@+id/nav_mie_recensioni"
        android:icon="@drawable/ic_review"
        android:title="@string/mie_recensioni"/>
    <item
        android:id="@+id/nav_persone_seguo"
        android:icon="@drawable/ic_person"
        android:title="@string/persone_seguo"/>
    <item
        android:id="@+id/nav_oggetti_seguo"
        android:icon="@drawable/ic_favorite"
        android:title="@string/oggetti_seguo"/>
    <item
        android:id="@+id/nav_inserisci_recensione"
        android:icon="@drawable/ic_review"
        android:title="@string/inserisci_recensione"/>
    <item
        android:id="@+id/nav_settings"
        android:icon="@drawable/ic_build"
        android:title="@string/action_settings"/>
    <item
        android:id="@+id/nav_esci"
        android:icon="@mipmap/ic_exit_to_app_black_24dp"
        android:title="@string/exit"/>
</group>

<item
    android:id="@+id/nav_Assistenza"
    android:icon="@drawable/ic_help"
    android:title="@string/Assistenza"/>

And superActivity:

  @Override
public boolean onNavigationItemSelected(MenuItem item) {
    Log.i("Successo","SOno entrato"+item.getItemId());
    // now set clicked menu item to checked
    Intent intent;
    switch(item.getItemId()) {
        case R.id.nav_home:
            intent = new Intent(ctx, A10_HomePage.class);
            break;
        case R.id.nav_registrati:
            intent = new Intent(ctx, A1_LoginIniziale.class);
            editor.putString("previous", "REG").apply();
            break;
        case R.id.nav_login:
            intent = new Intent(ctx, A1_LoginIniziale.class);
            editor.putString("previous", "LOGIN").apply();
            break;
        case R.id.nav_notifiche:
            intent = new Intent(ctx, A14_Notifiche.class);
            break;
        case R.id.nav_mie_recensioni:
            intent = new Intent(ctx, A23_MieRecensioni.class);
            break;
        case R.id.nav_oggetti_seguo:
            intent = new Intent(ctx, A22_OggettiSeguiti.class);
            break;
        case R.id.nav_persone_seguo:
            intent = new Intent(ctx, A19_PersoneSeguite.class);
            break;
        case R.id.nav_inserisci_recensione:
            intent = new Intent(ctx, A15_InserisciRecensione.class);
            break;
        case R.id.nav_settings:
            intent = new Intent(ctx, A6_Impostazioni.class);
            break;
        case R.id.nav_Assistenza:
            intent = new Intent(ctx, A35_Assistenza.class);
            break;
        case R.id.nav_esci:
            removeUser();
            intent = new Intent(ctx, A1_LoginIniziale.class);
            break;
        case R.id.nav_profilo:
            intent = new Intent(ctx, A7_ProfiloPrivato.class);
            break;
        default:
            intent = new Intent(ctx, A10_HomePage.class);
            break;
    }
    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    startActivity(intent);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

Layout:

<android.support.design.widget.NavigationView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:itemIconTint="@color/drawer_item_text"
        app:itemTextColor="@color/drawer_item_text"
        app:headerLayout="@layout/nav_header"/>

Solution

  • I have also found this problem and I solve this by using

    onNavigationItemSelected(navView.getMenu().getItem(R.id.nav_‌​profile));

    onNavigationItemSelected(navView.getMenu().getItem(0)); use this for first item selection by default.