user6487002
user6487002

Reputation:

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?

Upvotes: 8

Views: 3939

Answers (4)

Cătălin Florescu
Cătălin Florescu

Reputation: 5158

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

Upvotes: 0

Bill Duda
Bill Duda

Reputation: 111

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

Upvotes: 11

Dushyant Suthar
Dushyant Suthar

Reputation: 671

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.

Upvotes: 1

W4R10CK
W4R10CK

Reputation: 5550

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

Upvotes: 8

Related Questions