Android Navigation Drawer Remains Highlighted
Asked Answered
H

4

8

I am able to navigate from Activity1 to Activity2 via my navigation drawer.

But upon pressing the back button at activity2, the option remains highlighted.

My Code in activity1 is as followed

public boolean onNavigationItemSelected(MenuItem Item)
{
    int id = item.getItemId();

    if(id == R.id.activity2)
    {
        Intent goPage2 = new Intent(activity1.this, activity2.class);
        startActivity(goPage2);
    }
}

there is no code in activity 2.

May I know what do I do to remove the highlight?

Histrionism answered 15/1, 2017 at 10:34 Comment(4)
what do u mean by option remains highlighted. ?Migration
i.sstatic.net/GCfTs.pngHistrionism
this is an image i found online which matches my case.Histrionism
check out the answer i posted now,Migration
M
8

To uncheck the item click, you must pass false on checked item:

public boolean onNavigationItemSelected(MenuItem Item) //will consider to keep this in lower case - item
{
int id = item.getItemId();

 if(id == R.id.activity2)
 {
    Intent goPage2 = new Intent(activity1.this, activity2.class);
    startActivity(goPage2);
    Item.setChecked(false);  //pass false to uncheck
 }
}
Migration answered 15/1, 2017 at 10:57 Comment(3)
Thank you, I've been reading so many complicated solution and i didn't know mine was so simple. Thanks!!Histrionism
Cool man, one more thing change the Item to item on MenuItem Item .Migration
Noted, i'll change, the "item" was generated by android studio itself, but i shall change it now!Histrionism
B
11

I have found that simply return false, at the end of onNavigationItemSelected, if you have no need to use the highlight feature.

       } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_send) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return false;
}
Brickle answered 11/12, 2017 at 23:13 Comment(1)
This must be the accepted answer. The actual accepted one doesn't dehighlight at all.Bulley
M
8

To uncheck the item click, you must pass false on checked item:

public boolean onNavigationItemSelected(MenuItem Item) //will consider to keep this in lower case - item
{
int id = item.getItemId();

 if(id == R.id.activity2)
 {
    Intent goPage2 = new Intent(activity1.this, activity2.class);
    startActivity(goPage2);
    Item.setChecked(false);  //pass false to uncheck
 }
}
Migration answered 15/1, 2017 at 10:57 Comment(3)
Thank you, I've been reading so many complicated solution and i didn't know mine was so simple. Thanks!!Histrionism
Cool man, one more thing change the Item to item on MenuItem Item .Migration
Noted, i'll change, the "item" was generated by android studio itself, but i shall change it now!Histrionism
V
1

Of course it will remain highlighted("selected" more professional) because when you select an item from Navigation Drawer next activity is created and the activity in which the Navigation Drawer exists gets paused and stopped not destroyed (onPause()then onStop() are called) and remains in the memory till your device has enough memory. So, going to next activity does not consume much memory to force android to destroy previous activity. What you can do is either destroy your Navigation Drawer activity completely or you can uncheck all the items in the Navigation Drawer manually by writing this code for all items in onRestart() method of first activity.

menuItem1.setChecked(false);
menuItem2.setChecked(false);
menuItem3.setChecked(false);
menuItem4.setChecked(false);

and so on.

Vanhoose answered 15/1, 2017 at 11:8 Comment(0)
M
0

Using menuItem.setChecked(true) should check it and menuItem.setChecked(false) to uncheck it.

Multilateral answered 15/1, 2017 at 10:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.