0

In my android application I changed the back button functionality so that it goes to the main screen of my game , now that it's on the main screen how should I exit the whole application with back button ?

public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK ) {
          Assets.getInstance().getClick().play(1);
          this.clearScreenStack();
          this.setScreen(new MainMenuScreen(this));
        return true;
    }

    return super.onKeyDown(keyCode, event);
}
melisa zand
  • 201
  • 1
  • 6
  • 16

5 Answers5

1

Android's design does not favor exiting an application by choice, but rather manages it by the OS. You can bring up the Home application by its corresponding Intent:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

check How to exit from the application and show the home screen?

Community
  • 1
  • 1
Nermeen
  • 15,770
  • 5
  • 57
  • 71
1

If you have a mechanism that you can use to see which screen is showing you could do something like this:

public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK ) {
        if(mainScreenIsShowing == true){
            //If the main screen is showing let the back button
            //have its default behavior.
            return super.onKeyDown(keyCode, event);
        }else{
            Assets.getInstance().getClick().play(1);
            this.clearScreenStack();
            this.setScreen(new MainMenuScreen(this));
            return true;
        }

    }
    return super.onKeyDown(keyCode, event);
}
FoamyGuy
  • 46,067
  • 17
  • 123
  • 153
0

Google discourages exit buttons, you should let the OS handle it.

Give this a read: Is quitting an application frowned upon?

I can't help if you really really want to exit though, but I thought I'd mention this.

Community
  • 1
  • 1
JDM
  • 863
  • 1
  • 7
  • 20
  • Did you read the question? OP is asking about using the back button to get out of the application(which is the stock behavior for android), not implementing an exit button. – FoamyGuy Nov 28 '12 at 14:51
  • The OP wants exit button functionality on the back button. Basically the same. – user1810737 Nov 28 '12 at 14:52
  • but the default behavior of the back button is to call finish() on the top Activity in the stack... He is just trying to figure out how to make it have default behavior when the main screen is showing. – FoamyGuy Nov 28 '12 at 14:58
0

You can also check if the user enters the back button two times.

boolean backPressed = false;
    public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK && !backPressed) {
          Assets.getInstance().getClick().play(1);
          this.clearScreenStack();
          this.setScreen(new MainMenuScreen(this));
          backPressed = true;
        return true;
    }

    backPressed = false;
    return super.onKeyDown(keyCode, event);
}
Michiel Bijlsma
  • 563
  • 5
  • 10
0

This is a debateable subject, but I see nothing wrong or with an application exiting when the back button is pressed. After all, a call to finish() is the default behaviour of the back button. If the activity handling your main screen is at the bottom of your activity stack, then a call to finish() will exit your application.

I propose the following: Let your MainMenuScreen be handled in a separate activity, MainMenuActivity, which is the main activity. finish() other activities when going back to the MainMenuActivity, and handle onKeyDown like this in MainMenuActivity:

public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK ) {
        this.finish()
    }
}
Tore Rudberg
  • 1,544
  • 15
  • 16