Android menu shared between activities (unwanted)
I think I understood the Activity lifecycle on Android, but I still can't figure why the followin is happening.
- Login activity - Login screen
- M开发者_Go百科ain activity - Main application interface
In Login activity, I connect to a service to validate the login. If the password is correct, I switch to Main activity using:
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
There is no Options menu in Login activity. I have an Options menu in Main activity that I inflate with:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.option, menu);
return true;
}
So far so good. But I want the user to be able to log out and go back to login screen. So in Main activity I have an option in the menu that allows logging out:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menuReset:
getModel().deleteCredentials // stuff to logout from my service
startActivity(new Intent(this, LoginActivity.class));
finish();
return true;
}
}
If I press that menu button, the application displays Login activity again. But if I press the MENU key, the application displays the menu from Main activity even if it's not visible.
Furthermore, if I go back to home screen (pressing HOME) and come back to my activity after a while, the application displays the Login activity, but if I press the MENU key the Main application menu still shows.
How do I remove the menu from the Login activity?
Thanks
Oooooops, my bad. The inflate menu code was accidentally copied and pasted to Login Activity when I was coping another piece of code :)
Sorry about this Thanks anyway for the support!!
I think you don't need to call startActivity from your MainActivity to go to the Login Activity. Because the Activity chain, would be enough calling finish from your MainActivity.
精彩评论