Uncheck MenuItem in Navigation Drawer after return to main activity
I have a small problem with navigation drawer. I want to have nothing "checked" after return to main activity, here is more about my problem:
When I am starting an activity, I have menu like that:

That is good :) When I click "User profil" or other menu item I am opening new activity. I don't have any menu in this "new activity", only return button, so I am returning to main activity. And here is a problem, when I return to main activity and I open navigation drawer, it is looking like:

What I have to do to uncheck this menu item that I previous open?
//EDIT
Code of Navigation Drawer:
mToolbar = findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
ActionBar actionbar = this.getSupportActionBar();
actionbar.setDisplayHomeAsUpEnabled(true);
actionbar.setHomeAsUpIndicator(R.drawable.ic_menu_black_24dp);
mDrawerLayout = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
// close drawer when item is tapped
mDrawerLayout.closeDrawers();
switch (menuItem.getItemId()) {
case android.R.id.home:
mDrawerLayout.openDrawer(GravityCompat.START);
return true;
case R.id.action_user:
//Save info about clicked menu item to open correct activity when "onDrawerClosed" listener will be call
choseIntentFromDrawerLayout= EventContract.EventEntry.MENU_USER_PROFIL;
return true;
case R.id.action_settings:
choseIntentFromDrawerLayout=EventContract.EventEntry.MENU_SETTINGS;
return true;
case R.id.action_about:
choseIntentFromDrawerLayout= EventContract.EventEntry.MENU_ABOUT;
return true;
case R.id.action_log_out:
choseIntentFromDrawerLayout= EventContract.EventEntry.MENU_LOG_OUT;
return true;
default:
choseIntentFromDrawerLayout= EventContract.EventEntry.MENU_NOTHING;
}
//UNCHECK? - NO WORKING
if(menuItem.isChecked()){
menuItem.setChecked(false);
}
return true;
}
});
mDrawerLayout.addDrawerListener(
new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
// Respond when the drawer's position changes
}
@Override
public void onDrawerOpened(View drawerView) {
// Respond when the drawer is opened
choseIntentFromDrawerLayout= EventContract.EventEntry.MENU_NOTHING;
}
@Override
public void onDrawerClosed(View drawerView) {
// Respond when the drawer is closed
switch (choseIntentFromDrawerLayout) {
case EventContract.EventEntry.MENU_USER_PROFIL:
Intent intentUser = new Intent(CatalogActivity.this,UserActivity.class);
mExtraInfoHelp.putWindsurferToIntent(intentUser,mWindsurfer,getApplicationContext());
startActivity(intentUser);
break;
case EventContract.EventEntry.MENU_SETTINGS:
Intent intentSettings = new Intent(CatalogActivity.this,SettingsActivity.class);
startActivity(intentSettings);
break;
case EventContract.EventEntry.MENU_ABOUT:
Intent intentAbout = new Intent(CatalogActivity.this,AboutActivity.class);
startActivity(intentAbout);
break;
case EventContract.EventEntry.MENU_LOG_OUT:
AuthUI.getInstance().signOut(CatalogActivity.this);
break;
case EventContract.EventEntry.MENU_NOTHING:
break;
}
}
@Override
public void onDrawerStateChanged(int newState) {
// Respond when the drawer motion state changes
}
}
);
|
show 1 more comment
I have a small problem with navigation drawer. I want to have nothing "checked" after return to main activity, here is more about my problem:
When I am starting an activity, I have menu like that:

That is good :) When I click "User profil" or other menu item I am opening new activity. I don't have any menu in this "new activity", only return button, so I am returning to main activity. And here is a problem, when I return to main activity and I open navigation drawer, it is looking like:

What I have to do to uncheck this menu item that I previous open?
//EDIT
Code of Navigation Drawer:
mToolbar = findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
ActionBar actionbar = this.getSupportActionBar();
actionbar.setDisplayHomeAsUpEnabled(true);
actionbar.setHomeAsUpIndicator(R.drawable.ic_menu_black_24dp);
mDrawerLayout = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
// close drawer when item is tapped
mDrawerLayout.closeDrawers();
switch (menuItem.getItemId()) {
case android.R.id.home:
mDrawerLayout.openDrawer(GravityCompat.START);
return true;
case R.id.action_user:
//Save info about clicked menu item to open correct activity when "onDrawerClosed" listener will be call
choseIntentFromDrawerLayout= EventContract.EventEntry.MENU_USER_PROFIL;
return true;
case R.id.action_settings:
choseIntentFromDrawerLayout=EventContract.EventEntry.MENU_SETTINGS;
return true;
case R.id.action_about:
choseIntentFromDrawerLayout= EventContract.EventEntry.MENU_ABOUT;
return true;
case R.id.action_log_out:
choseIntentFromDrawerLayout= EventContract.EventEntry.MENU_LOG_OUT;
return true;
default:
choseIntentFromDrawerLayout= EventContract.EventEntry.MENU_NOTHING;
}
//UNCHECK? - NO WORKING
if(menuItem.isChecked()){
menuItem.setChecked(false);
}
return true;
}
});
mDrawerLayout.addDrawerListener(
new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
// Respond when the drawer's position changes
}
@Override
public void onDrawerOpened(View drawerView) {
// Respond when the drawer is opened
choseIntentFromDrawerLayout= EventContract.EventEntry.MENU_NOTHING;
}
@Override
public void onDrawerClosed(View drawerView) {
// Respond when the drawer is closed
switch (choseIntentFromDrawerLayout) {
case EventContract.EventEntry.MENU_USER_PROFIL:
Intent intentUser = new Intent(CatalogActivity.this,UserActivity.class);
mExtraInfoHelp.putWindsurferToIntent(intentUser,mWindsurfer,getApplicationContext());
startActivity(intentUser);
break;
case EventContract.EventEntry.MENU_SETTINGS:
Intent intentSettings = new Intent(CatalogActivity.this,SettingsActivity.class);
startActivity(intentSettings);
break;
case EventContract.EventEntry.MENU_ABOUT:
Intent intentAbout = new Intent(CatalogActivity.this,AboutActivity.class);
startActivity(intentAbout);
break;
case EventContract.EventEntry.MENU_LOG_OUT:
AuthUI.getInstance().signOut(CatalogActivity.this);
break;
case EventContract.EventEntry.MENU_NOTHING:
break;
}
}
@Override
public void onDrawerStateChanged(int newState) {
// Respond when the drawer motion state changes
}
}
);
1
How about unchecking everything in "onPause" or "onResume" method of the activity with the menu? Or uncheck your menu item directly in your "onClick" listener, after you start the new activity.
– giro
Nov 14 '18 at 23:16
With what method? I am using "menuItem.setChecked(false);" and it doesn't working. Is another possibility?
– Andropogon
Nov 14 '18 at 23:19
1
Could you post the code related to your menu?
– giro
Nov 14 '18 at 23:21
I added it to the previous post.
– Andropogon
Nov 14 '18 at 23:29
1
As mentioned before, I'd uncheck your menuItems in the "onPause" or "onResume" method. I'd assume that your call to menuItem.isChecked returns false, because android will set your menuItem as checked after the listener was called. If this assumption is true then the second part of my first comment would be wrong and you can't uncheck your menu item inside the listener as you did. The answer of arsent shows how to uncheck menu items: stackoverflow.com/questions/36051045/…
– giro
Nov 14 '18 at 23:45
|
show 1 more comment
I have a small problem with navigation drawer. I want to have nothing "checked" after return to main activity, here is more about my problem:
When I am starting an activity, I have menu like that:

That is good :) When I click "User profil" or other menu item I am opening new activity. I don't have any menu in this "new activity", only return button, so I am returning to main activity. And here is a problem, when I return to main activity and I open navigation drawer, it is looking like:

What I have to do to uncheck this menu item that I previous open?
//EDIT
Code of Navigation Drawer:
mToolbar = findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
ActionBar actionbar = this.getSupportActionBar();
actionbar.setDisplayHomeAsUpEnabled(true);
actionbar.setHomeAsUpIndicator(R.drawable.ic_menu_black_24dp);
mDrawerLayout = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
// close drawer when item is tapped
mDrawerLayout.closeDrawers();
switch (menuItem.getItemId()) {
case android.R.id.home:
mDrawerLayout.openDrawer(GravityCompat.START);
return true;
case R.id.action_user:
//Save info about clicked menu item to open correct activity when "onDrawerClosed" listener will be call
choseIntentFromDrawerLayout= EventContract.EventEntry.MENU_USER_PROFIL;
return true;
case R.id.action_settings:
choseIntentFromDrawerLayout=EventContract.EventEntry.MENU_SETTINGS;
return true;
case R.id.action_about:
choseIntentFromDrawerLayout= EventContract.EventEntry.MENU_ABOUT;
return true;
case R.id.action_log_out:
choseIntentFromDrawerLayout= EventContract.EventEntry.MENU_LOG_OUT;
return true;
default:
choseIntentFromDrawerLayout= EventContract.EventEntry.MENU_NOTHING;
}
//UNCHECK? - NO WORKING
if(menuItem.isChecked()){
menuItem.setChecked(false);
}
return true;
}
});
mDrawerLayout.addDrawerListener(
new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
// Respond when the drawer's position changes
}
@Override
public void onDrawerOpened(View drawerView) {
// Respond when the drawer is opened
choseIntentFromDrawerLayout= EventContract.EventEntry.MENU_NOTHING;
}
@Override
public void onDrawerClosed(View drawerView) {
// Respond when the drawer is closed
switch (choseIntentFromDrawerLayout) {
case EventContract.EventEntry.MENU_USER_PROFIL:
Intent intentUser = new Intent(CatalogActivity.this,UserActivity.class);
mExtraInfoHelp.putWindsurferToIntent(intentUser,mWindsurfer,getApplicationContext());
startActivity(intentUser);
break;
case EventContract.EventEntry.MENU_SETTINGS:
Intent intentSettings = new Intent(CatalogActivity.this,SettingsActivity.class);
startActivity(intentSettings);
break;
case EventContract.EventEntry.MENU_ABOUT:
Intent intentAbout = new Intent(CatalogActivity.this,AboutActivity.class);
startActivity(intentAbout);
break;
case EventContract.EventEntry.MENU_LOG_OUT:
AuthUI.getInstance().signOut(CatalogActivity.this);
break;
case EventContract.EventEntry.MENU_NOTHING:
break;
}
}
@Override
public void onDrawerStateChanged(int newState) {
// Respond when the drawer motion state changes
}
}
);
I have a small problem with navigation drawer. I want to have nothing "checked" after return to main activity, here is more about my problem:
When I am starting an activity, I have menu like that:

That is good :) When I click "User profil" or other menu item I am opening new activity. I don't have any menu in this "new activity", only return button, so I am returning to main activity. And here is a problem, when I return to main activity and I open navigation drawer, it is looking like:

What I have to do to uncheck this menu item that I previous open?
//EDIT
Code of Navigation Drawer:
mToolbar = findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
ActionBar actionbar = this.getSupportActionBar();
actionbar.setDisplayHomeAsUpEnabled(true);
actionbar.setHomeAsUpIndicator(R.drawable.ic_menu_black_24dp);
mDrawerLayout = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
// close drawer when item is tapped
mDrawerLayout.closeDrawers();
switch (menuItem.getItemId()) {
case android.R.id.home:
mDrawerLayout.openDrawer(GravityCompat.START);
return true;
case R.id.action_user:
//Save info about clicked menu item to open correct activity when "onDrawerClosed" listener will be call
choseIntentFromDrawerLayout= EventContract.EventEntry.MENU_USER_PROFIL;
return true;
case R.id.action_settings:
choseIntentFromDrawerLayout=EventContract.EventEntry.MENU_SETTINGS;
return true;
case R.id.action_about:
choseIntentFromDrawerLayout= EventContract.EventEntry.MENU_ABOUT;
return true;
case R.id.action_log_out:
choseIntentFromDrawerLayout= EventContract.EventEntry.MENU_LOG_OUT;
return true;
default:
choseIntentFromDrawerLayout= EventContract.EventEntry.MENU_NOTHING;
}
//UNCHECK? - NO WORKING
if(menuItem.isChecked()){
menuItem.setChecked(false);
}
return true;
}
});
mDrawerLayout.addDrawerListener(
new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
// Respond when the drawer's position changes
}
@Override
public void onDrawerOpened(View drawerView) {
// Respond when the drawer is opened
choseIntentFromDrawerLayout= EventContract.EventEntry.MENU_NOTHING;
}
@Override
public void onDrawerClosed(View drawerView) {
// Respond when the drawer is closed
switch (choseIntentFromDrawerLayout) {
case EventContract.EventEntry.MENU_USER_PROFIL:
Intent intentUser = new Intent(CatalogActivity.this,UserActivity.class);
mExtraInfoHelp.putWindsurferToIntent(intentUser,mWindsurfer,getApplicationContext());
startActivity(intentUser);
break;
case EventContract.EventEntry.MENU_SETTINGS:
Intent intentSettings = new Intent(CatalogActivity.this,SettingsActivity.class);
startActivity(intentSettings);
break;
case EventContract.EventEntry.MENU_ABOUT:
Intent intentAbout = new Intent(CatalogActivity.this,AboutActivity.class);
startActivity(intentAbout);
break;
case EventContract.EventEntry.MENU_LOG_OUT:
AuthUI.getInstance().signOut(CatalogActivity.this);
break;
case EventContract.EventEntry.MENU_NOTHING:
break;
}
}
@Override
public void onDrawerStateChanged(int newState) {
// Respond when the drawer motion state changes
}
}
);
edited Nov 14 '18 at 23:28
Andropogon
asked Nov 14 '18 at 23:06
AndropogonAndropogon
279
279
1
How about unchecking everything in "onPause" or "onResume" method of the activity with the menu? Or uncheck your menu item directly in your "onClick" listener, after you start the new activity.
– giro
Nov 14 '18 at 23:16
With what method? I am using "menuItem.setChecked(false);" and it doesn't working. Is another possibility?
– Andropogon
Nov 14 '18 at 23:19
1
Could you post the code related to your menu?
– giro
Nov 14 '18 at 23:21
I added it to the previous post.
– Andropogon
Nov 14 '18 at 23:29
1
As mentioned before, I'd uncheck your menuItems in the "onPause" or "onResume" method. I'd assume that your call to menuItem.isChecked returns false, because android will set your menuItem as checked after the listener was called. If this assumption is true then the second part of my first comment would be wrong and you can't uncheck your menu item inside the listener as you did. The answer of arsent shows how to uncheck menu items: stackoverflow.com/questions/36051045/…
– giro
Nov 14 '18 at 23:45
|
show 1 more comment
1
How about unchecking everything in "onPause" or "onResume" method of the activity with the menu? Or uncheck your menu item directly in your "onClick" listener, after you start the new activity.
– giro
Nov 14 '18 at 23:16
With what method? I am using "menuItem.setChecked(false);" and it doesn't working. Is another possibility?
– Andropogon
Nov 14 '18 at 23:19
1
Could you post the code related to your menu?
– giro
Nov 14 '18 at 23:21
I added it to the previous post.
– Andropogon
Nov 14 '18 at 23:29
1
As mentioned before, I'd uncheck your menuItems in the "onPause" or "onResume" method. I'd assume that your call to menuItem.isChecked returns false, because android will set your menuItem as checked after the listener was called. If this assumption is true then the second part of my first comment would be wrong and you can't uncheck your menu item inside the listener as you did. The answer of arsent shows how to uncheck menu items: stackoverflow.com/questions/36051045/…
– giro
Nov 14 '18 at 23:45
1
1
How about unchecking everything in "onPause" or "onResume" method of the activity with the menu? Or uncheck your menu item directly in your "onClick" listener, after you start the new activity.
– giro
Nov 14 '18 at 23:16
How about unchecking everything in "onPause" or "onResume" method of the activity with the menu? Or uncheck your menu item directly in your "onClick" listener, after you start the new activity.
– giro
Nov 14 '18 at 23:16
With what method? I am using "menuItem.setChecked(false);" and it doesn't working. Is another possibility?
– Andropogon
Nov 14 '18 at 23:19
With what method? I am using "menuItem.setChecked(false);" and it doesn't working. Is another possibility?
– Andropogon
Nov 14 '18 at 23:19
1
1
Could you post the code related to your menu?
– giro
Nov 14 '18 at 23:21
Could you post the code related to your menu?
– giro
Nov 14 '18 at 23:21
I added it to the previous post.
– Andropogon
Nov 14 '18 at 23:29
I added it to the previous post.
– Andropogon
Nov 14 '18 at 23:29
1
1
As mentioned before, I'd uncheck your menuItems in the "onPause" or "onResume" method. I'd assume that your call to menuItem.isChecked returns false, because android will set your menuItem as checked after the listener was called. If this assumption is true then the second part of my first comment would be wrong and you can't uncheck your menu item inside the listener as you did. The answer of arsent shows how to uncheck menu items: stackoverflow.com/questions/36051045/…
– giro
Nov 14 '18 at 23:45
As mentioned before, I'd uncheck your menuItems in the "onPause" or "onResume" method. I'd assume that your call to menuItem.isChecked returns false, because android will set your menuItem as checked after the listener was called. If this assumption is true then the second part of my first comment would be wrong and you can't uncheck your menu item inside the listener as you did. The answer of arsent shows how to uncheck menu items: stackoverflow.com/questions/36051045/…
– giro
Nov 14 '18 at 23:45
|
show 1 more comment
2 Answers
2
active
oldest
votes
I'd uncheck your menuItems in the "onPause" or "onResume" method. I'd assume that your call to menuItem.isChecked returns false, because android will set your menuItem as checked after the listener was called. The answer by arsent shows how to uncheck menu items: How to uncheck checked items in Navigation View?
It is also possible to uncheck menu items in onDrawerClosed listner:
This code works:
@Override
public void onDrawerClosed(View drawerView) {
//Solution:
int size = navigationView.getMenu().size();
for (int i = 0; i < size; i++) {
navigationView.getMenu().getItem(i).setChecked(false);
}
// Respond when the drawer is closed
switch (choseIntentFromDrawerLayout) {
case EventContract.EventEntry.MENU_USER_PROFIL:
Intent intentUser = new Intent(CatalogActivity.this,UserActivity.class);
mExtraInfoHelp.putWindsurferToIntent(intentUser,mWindsurfer,getApplicationContext());
startActivity(intentUser);
break;
case EventContract.EventEntry.MENU_SETTINGS:
Intent intentSettings = new Intent(CatalogActivity.this,SettingsActivity.class);
startActivity(intentSettings);
break;
case EventContract.EventEntry.MENU_ABOUT:
Intent intentAbout = new Intent(CatalogActivity.this,AboutActivity.class);
startActivity(intentAbout);
break;
case EventContract.EventEntry.MENU_LOG_OUT:
AuthUI.getInstance().signOut(CatalogActivity.this);
break;
case EventContract.EventEntry.MENU_NOTHING:
break;
}
}
add a comment |
Firstly, Let's create an int variable to get the size of your navigation menu.
int size = navigationView.getMenu().size();
Second, make a for loop to un-check all the menu items in your navigation view
for (int i = 0; i < size; i++)
navigationView.getMenu().getItem(i).setChecked(false);
Third, let's put our two steps of code block into onNavigationItemSelected block. Your code will be looks like this.
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
// close drawer when item is tapped
int size = navigationView.getMenu().size();
for (int i = 0; i < size; i++)
navigationView.getMenu().getItem(i).setChecked(false);
//Your switch code here.
}
Finally, let's put menuItem.setChecked(true); and drawerLayout.closeDrawers(); in every case of your switch fun. And then do all the work you did in onDrawerClosed in onNavigationItemSelected. Delete all the code in onDrawerClosed. As for me, I love to work all the code in single block. We can create another fun for reducing code lines.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310076%2funcheck-menuitem-in-navigation-drawer-after-return-to-main-activity%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I'd uncheck your menuItems in the "onPause" or "onResume" method. I'd assume that your call to menuItem.isChecked returns false, because android will set your menuItem as checked after the listener was called. The answer by arsent shows how to uncheck menu items: How to uncheck checked items in Navigation View?
It is also possible to uncheck menu items in onDrawerClosed listner:
This code works:
@Override
public void onDrawerClosed(View drawerView) {
//Solution:
int size = navigationView.getMenu().size();
for (int i = 0; i < size; i++) {
navigationView.getMenu().getItem(i).setChecked(false);
}
// Respond when the drawer is closed
switch (choseIntentFromDrawerLayout) {
case EventContract.EventEntry.MENU_USER_PROFIL:
Intent intentUser = new Intent(CatalogActivity.this,UserActivity.class);
mExtraInfoHelp.putWindsurferToIntent(intentUser,mWindsurfer,getApplicationContext());
startActivity(intentUser);
break;
case EventContract.EventEntry.MENU_SETTINGS:
Intent intentSettings = new Intent(CatalogActivity.this,SettingsActivity.class);
startActivity(intentSettings);
break;
case EventContract.EventEntry.MENU_ABOUT:
Intent intentAbout = new Intent(CatalogActivity.this,AboutActivity.class);
startActivity(intentAbout);
break;
case EventContract.EventEntry.MENU_LOG_OUT:
AuthUI.getInstance().signOut(CatalogActivity.this);
break;
case EventContract.EventEntry.MENU_NOTHING:
break;
}
}
add a comment |
I'd uncheck your menuItems in the "onPause" or "onResume" method. I'd assume that your call to menuItem.isChecked returns false, because android will set your menuItem as checked after the listener was called. The answer by arsent shows how to uncheck menu items: How to uncheck checked items in Navigation View?
It is also possible to uncheck menu items in onDrawerClosed listner:
This code works:
@Override
public void onDrawerClosed(View drawerView) {
//Solution:
int size = navigationView.getMenu().size();
for (int i = 0; i < size; i++) {
navigationView.getMenu().getItem(i).setChecked(false);
}
// Respond when the drawer is closed
switch (choseIntentFromDrawerLayout) {
case EventContract.EventEntry.MENU_USER_PROFIL:
Intent intentUser = new Intent(CatalogActivity.this,UserActivity.class);
mExtraInfoHelp.putWindsurferToIntent(intentUser,mWindsurfer,getApplicationContext());
startActivity(intentUser);
break;
case EventContract.EventEntry.MENU_SETTINGS:
Intent intentSettings = new Intent(CatalogActivity.this,SettingsActivity.class);
startActivity(intentSettings);
break;
case EventContract.EventEntry.MENU_ABOUT:
Intent intentAbout = new Intent(CatalogActivity.this,AboutActivity.class);
startActivity(intentAbout);
break;
case EventContract.EventEntry.MENU_LOG_OUT:
AuthUI.getInstance().signOut(CatalogActivity.this);
break;
case EventContract.EventEntry.MENU_NOTHING:
break;
}
}
add a comment |
I'd uncheck your menuItems in the "onPause" or "onResume" method. I'd assume that your call to menuItem.isChecked returns false, because android will set your menuItem as checked after the listener was called. The answer by arsent shows how to uncheck menu items: How to uncheck checked items in Navigation View?
It is also possible to uncheck menu items in onDrawerClosed listner:
This code works:
@Override
public void onDrawerClosed(View drawerView) {
//Solution:
int size = navigationView.getMenu().size();
for (int i = 0; i < size; i++) {
navigationView.getMenu().getItem(i).setChecked(false);
}
// Respond when the drawer is closed
switch (choseIntentFromDrawerLayout) {
case EventContract.EventEntry.MENU_USER_PROFIL:
Intent intentUser = new Intent(CatalogActivity.this,UserActivity.class);
mExtraInfoHelp.putWindsurferToIntent(intentUser,mWindsurfer,getApplicationContext());
startActivity(intentUser);
break;
case EventContract.EventEntry.MENU_SETTINGS:
Intent intentSettings = new Intent(CatalogActivity.this,SettingsActivity.class);
startActivity(intentSettings);
break;
case EventContract.EventEntry.MENU_ABOUT:
Intent intentAbout = new Intent(CatalogActivity.this,AboutActivity.class);
startActivity(intentAbout);
break;
case EventContract.EventEntry.MENU_LOG_OUT:
AuthUI.getInstance().signOut(CatalogActivity.this);
break;
case EventContract.EventEntry.MENU_NOTHING:
break;
}
}
I'd uncheck your menuItems in the "onPause" or "onResume" method. I'd assume that your call to menuItem.isChecked returns false, because android will set your menuItem as checked after the listener was called. The answer by arsent shows how to uncheck menu items: How to uncheck checked items in Navigation View?
It is also possible to uncheck menu items in onDrawerClosed listner:
This code works:
@Override
public void onDrawerClosed(View drawerView) {
//Solution:
int size = navigationView.getMenu().size();
for (int i = 0; i < size; i++) {
navigationView.getMenu().getItem(i).setChecked(false);
}
// Respond when the drawer is closed
switch (choseIntentFromDrawerLayout) {
case EventContract.EventEntry.MENU_USER_PROFIL:
Intent intentUser = new Intent(CatalogActivity.this,UserActivity.class);
mExtraInfoHelp.putWindsurferToIntent(intentUser,mWindsurfer,getApplicationContext());
startActivity(intentUser);
break;
case EventContract.EventEntry.MENU_SETTINGS:
Intent intentSettings = new Intent(CatalogActivity.this,SettingsActivity.class);
startActivity(intentSettings);
break;
case EventContract.EventEntry.MENU_ABOUT:
Intent intentAbout = new Intent(CatalogActivity.this,AboutActivity.class);
startActivity(intentAbout);
break;
case EventContract.EventEntry.MENU_LOG_OUT:
AuthUI.getInstance().signOut(CatalogActivity.this);
break;
case EventContract.EventEntry.MENU_NOTHING:
break;
}
}
edited Nov 15 '18 at 2:07
Mozahler
2,16151826
2,16151826
answered Nov 15 '18 at 0:03
girogiro
3711615
3711615
add a comment |
add a comment |
Firstly, Let's create an int variable to get the size of your navigation menu.
int size = navigationView.getMenu().size();
Second, make a for loop to un-check all the menu items in your navigation view
for (int i = 0; i < size; i++)
navigationView.getMenu().getItem(i).setChecked(false);
Third, let's put our two steps of code block into onNavigationItemSelected block. Your code will be looks like this.
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
// close drawer when item is tapped
int size = navigationView.getMenu().size();
for (int i = 0; i < size; i++)
navigationView.getMenu().getItem(i).setChecked(false);
//Your switch code here.
}
Finally, let's put menuItem.setChecked(true); and drawerLayout.closeDrawers(); in every case of your switch fun. And then do all the work you did in onDrawerClosed in onNavigationItemSelected. Delete all the code in onDrawerClosed. As for me, I love to work all the code in single block. We can create another fun for reducing code lines.
add a comment |
Firstly, Let's create an int variable to get the size of your navigation menu.
int size = navigationView.getMenu().size();
Second, make a for loop to un-check all the menu items in your navigation view
for (int i = 0; i < size; i++)
navigationView.getMenu().getItem(i).setChecked(false);
Third, let's put our two steps of code block into onNavigationItemSelected block. Your code will be looks like this.
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
// close drawer when item is tapped
int size = navigationView.getMenu().size();
for (int i = 0; i < size; i++)
navigationView.getMenu().getItem(i).setChecked(false);
//Your switch code here.
}
Finally, let's put menuItem.setChecked(true); and drawerLayout.closeDrawers(); in every case of your switch fun. And then do all the work you did in onDrawerClosed in onNavigationItemSelected. Delete all the code in onDrawerClosed. As for me, I love to work all the code in single block. We can create another fun for reducing code lines.
add a comment |
Firstly, Let's create an int variable to get the size of your navigation menu.
int size = navigationView.getMenu().size();
Second, make a for loop to un-check all the menu items in your navigation view
for (int i = 0; i < size; i++)
navigationView.getMenu().getItem(i).setChecked(false);
Third, let's put our two steps of code block into onNavigationItemSelected block. Your code will be looks like this.
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
// close drawer when item is tapped
int size = navigationView.getMenu().size();
for (int i = 0; i < size; i++)
navigationView.getMenu().getItem(i).setChecked(false);
//Your switch code here.
}
Finally, let's put menuItem.setChecked(true); and drawerLayout.closeDrawers(); in every case of your switch fun. And then do all the work you did in onDrawerClosed in onNavigationItemSelected. Delete all the code in onDrawerClosed. As for me, I love to work all the code in single block. We can create another fun for reducing code lines.
Firstly, Let's create an int variable to get the size of your navigation menu.
int size = navigationView.getMenu().size();
Second, make a for loop to un-check all the menu items in your navigation view
for (int i = 0; i < size; i++)
navigationView.getMenu().getItem(i).setChecked(false);
Third, let's put our two steps of code block into onNavigationItemSelected block. Your code will be looks like this.
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
// close drawer when item is tapped
int size = navigationView.getMenu().size();
for (int i = 0; i < size; i++)
navigationView.getMenu().getItem(i).setChecked(false);
//Your switch code here.
}
Finally, let's put menuItem.setChecked(true); and drawerLayout.closeDrawers(); in every case of your switch fun. And then do all the work you did in onDrawerClosed in onNavigationItemSelected. Delete all the code in onDrawerClosed. As for me, I love to work all the code in single block. We can create another fun for reducing code lines.
answered Dec 2 '18 at 8:25
MinnKhantMinnKhant
214
214
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310076%2funcheck-menuitem-in-navigation-drawer-after-return-to-main-activity%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
How about unchecking everything in "onPause" or "onResume" method of the activity with the menu? Or uncheck your menu item directly in your "onClick" listener, after you start the new activity.
– giro
Nov 14 '18 at 23:16
With what method? I am using "menuItem.setChecked(false);" and it doesn't working. Is another possibility?
– Andropogon
Nov 14 '18 at 23:19
1
Could you post the code related to your menu?
– giro
Nov 14 '18 at 23:21
I added it to the previous post.
– Andropogon
Nov 14 '18 at 23:29
1
As mentioned before, I'd uncheck your menuItems in the "onPause" or "onResume" method. I'd assume that your call to menuItem.isChecked returns false, because android will set your menuItem as checked after the listener was called. If this assumption is true then the second part of my first comment would be wrong and you can't uncheck your menu item inside the listener as you did. The answer of arsent shows how to uncheck menu items: stackoverflow.com/questions/36051045/…
– giro
Nov 14 '18 at 23:45