My app is currently stuck in an endless loop with a physical return button. The program is configured to download the screensaver, and then go to the main menu. As soon as in the main menu the user can switch to another action of his choice. For example: New game activity. As soon as the user starts a new game, I want them to be able to click the "Back" button and transfer them to the main menu. Returning to the main menu, if they pressed the "Back" button again, I would like him to leave the game.
This is what I use for each activity:
public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK)) { startActivity(new Intent(NewGameActivity.this, MenuActivity.class)); } return super.onKeyDown(keyCode, event); }
It works correctly and returns the user to the main menu without problems. But if the user clicks the Back button again once in the main menu, he will bring them to the screen from which they just escaped. Thus, it returns to the previous screen each time.
Here's how the main menu is configured:
public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK)) { stopService(new Intent(MenuActivity.this, BGMusicService.class)); MenuActivity.this.finish(); } return super.onKeyDown(keyCode, event); }
If I press the "Back" button, as soon as the main menu loads for the first time, it will work correctly and close the game. It will only go bad if I press the back button earlier to go from one screen to the main menu.
Update:
Good thing it worked. When I press the back button on the main screen, the music stops and it acts as if it is trying to close the application, but returns to the main screen again. I have to hit him twice every time.
source share