0

I want to exit from application after click back in android phone.Actually i do not know the exact word for that back option.Screen shot of back option

I really know the way to exit from application after click button.So how can i code for that?can i do that?

4 Answers4

0

If you already know the logic and attached it to a button, you can override

@Override
public void onBackPressed() {

}

in your activity, and put the same logic in there

Terry W
  • 203
  • 3
  • 9
0

Quoting from this question:

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);

Note that his will not kill your app process, but rather put your activity on the background. As for when to trigger this code, you should override the onBackPressed() method for your current activity.

Community
  • 1
  • 1
frankelot
  • 13,666
  • 16
  • 54
  • 89
0

Simply by this code (if you only have just one activity remining in your current backstack.) corrected by feresr

@Override
public void onBackPressed() {
// your code, what to do under back arrow button.
    finish();  // to finish app or activity

}
Jakub S.
  • 5,580
  • 2
  • 42
  • 37
0

In your activity you can override method onBackPressed:

@Override
public void onBackPressed() {
  finish();
}

If you have stack of activities there are better way:

@Override
public void onBackPressed() {
  moveTaskToBack(true);
  android.os.Process.killProcess(android.os.Process.myPid());
  System.exit(1);
}
Peter Zverkov
  • 145
  • 1
  • 6