Is the back button stuck in an endless loop?

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.

+4
source share
3 answers

You must call finish() inside the onKeyDown method of your actions so that your current activity is actually removed from the stack.

Something like this should work:

 public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { startActivity(new Intent(NewGameActivity.this, MenuActivity.class)); finish(); } return super.onKeyDown(keyCode, event); } 
+2
source

First (from the comments) add return true; into if (to indicate that you handled the event), for example:

 // Main menu code public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { stopService(new Intent(MenuActivity.this, BGMusicService.class)); MenuActivity.this.finish(); return true; // Add me! } return super.onKeyDown(keyCode, event); } 

In addition, I believe that since you are starting the menu action again, you need to clear the back stack (the procedure used so far) before opening it. You should achieve this using Intent.FLAG_ACTIVITY_CLEAR_TOP (or perhaps Intent.FLAG_ACTIVITY_CLEAR_TASK ). Something like that:

 // Other activity code public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { Intent menuIntent = new Intent(NewGameActivity.this, MenuActivity.class); menuIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // Add me! startActivity(menuIntent); return true; // Add me, too! } return super.onKeyDown(keyCode, event); } 
+1
source

You did not call finish() in your other actions, so when you call finish() on a menu, it just closes and another activity is exposed

0
source

All Articles