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?

4
  • what do u mean by option remains highlighted. ?
    – W4R10CK
    Jan 15, 2017 at 10:36
  • i.stack.imgur.com/GCfTs.png
    – user6487002
    Jan 15, 2017 at 10:38
  • this is an image i found online which matches my case.
    – user6487002
    Jan 15, 2017 at 10:38
  • check out the answer i posted now,
    – W4R10CK
    Jan 15, 2017 at 10:58

4 Answers 4

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;
}
1
  • 1
    This must be the accepted answer. The actual accepted one doesn't dehighlight at all. Mar 27, 2019 at 13:36
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
 }
}
3
  • Thank you, I've been reading so many complicated solution and i didn't know mine was so simple. Thanks!!
    – user6487002
    Jan 15, 2017 at 11:00
  • Cool man, one more thing change the Item to item on MenuItem Item .
    – W4R10CK
    Jan 15, 2017 at 11:02
  • 1
    Noted, i'll change, the "item" was generated by android studio itself, but i shall change it now!
    – user6487002
    Jan 15, 2017 at 11:08
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.

0

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.