0

I would like my application, when launched to check wether a username and password are saved in sharedPreferences. If it detects those credentials, a login page is shown, if it doesn't, a registration page is shown. After the user logs in to the app I would like for the entire app to close wether the device's back button is pushed or the apps exit button is pushed.

I have everything working besides the apps closing properly. I thought I coded it correctly but I didn't. How can I get the entire app to close when the exit and device back button are selected.

exit method:

      public void onClick(DialogInterface dialog, int which) {
      // TODO Auto-generated method stub
      finish();
      }
      });
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface arg0, int arg1) {
               arg0.cancel();
            }
        });
    AlertDialog alert=builder.create();
    alert.show();
sean
  • 133
  • 8
  • 20
  • 1
    http://stackoverflow.com/questions/4617049/how-to-exit-from-an-android-app – Sean Dawson Sep 26 '12 at 22:43
  • There are very few cases where you ever want to include an exit button in an android app: http://blog.radioactiveyak.com/2010/05/when-to-include-exit-button-in-android.html – adamp Sep 27 '12 at 01:14

3 Answers3

3

Simply call finish() on your activity when the back button is pressed.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        finish();
    }
    return super.onKeyDown(keyCode, event);
}
Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134
  • Why would you override onKeyDown for this, instead of making use of the activity life cycle methods like onWindowFocus or onPause? – mah Sep 27 '12 at 00:10
  • the author specifically asked how to take action when back is pressed. if you don't think they really meant that, i invite you to open a dialog with them on the subject. – Jeffrey Blattman Sep 27 '12 at 01:19
  • There's an explicit onBackPressed method that would be more appropriate to override. And the default already calls finish(). – adamp Sep 27 '12 at 01:32
  • @Jeffrey when the back button is pressed, both of the lifecycle methods I specified will get called. – mah Sep 27 '12 at 03:13
1
public void AppExit()
{

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

/*int pid = android.os.Process.myPid();=====> use this if you want to kill your activity. But its not a good one to do.
 android.os.Process.killProcess(pid);*/

 }

call this method when you want to exit the app.

for suppose you want to exit the app whenever back button is pressed just do like this

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
    AppExit();
}
return super.onKeyDown(keyCode, event);
}
Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
1

You can use this code to close or exit your application.

android.os.Process.killProcess(android.os.Process.myPid());
AmmY
  • 1,821
  • 1
  • 20
  • 31