I have a menu in my toolbar which has two submenus (Login, Log out). When i press on the menuicon and the user is not logged in i want it to open another activity immediately. When the User is logged in it should open the submenu-list.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/fieldChooserMenu">
<item android:id="@+id/userButton"
android:title="@string/userButton"
android:icon="@drawable/ic_person_outline_black_24dp"
app:showAsAction="always">
<menu>
<item android:id="@+id/changeUser" android:title="Login"/>
<item android:id="@+id/logOutUser" android:title="Log out"/>
</menu>
</item>
</menu>
Here is an example of what should be done in onOptionsItemselected:
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.userButton) {
if (userIsLoggedIn()) {
//Opens other options (Change User && Logout User)
} else {
openUserManagementActivity();
}
} else if (item.getItemId() == R.id.changeUser) {
openUserManagementActivity();
} else if (item.getItemId() == R.id.logOutUser) {
logoutUser(this);
}
return true;
}
The problem is that the submenu list is opened either way.
Is there a way to close the menu (or keep it from opening)?
Thanks in advance!