androidandroid-navigationandroid-drawer

Android Navigation Drawer Remains Highlighted


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?


Solution

  • 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
     }
    }