When you go from A activity to B activity, call finish()
.so in android stack
A activity ll get finish() or get killed
. when you press back or exit button on top activity then stacks gets empty, then you ll go to menu screen or home screen.
for example : when your going from A activity to B activity perform below step:
startActivity(new Intent(A.this, B.class));
finish();
so your A activity gets finish, & your new B activity gets started. Here when you press back button or exit button you ll go to home screen or menu screen.
Here we have two methods to finish or kill
the app on back button pressed.
Using finish()
which closes or finishes the current activity on android screen.
for example : you have 2 activities i.e A & B. You ll go from A activity to B activity using intent, now foreground acitivity is B, you want to go back & kill B activity & goto A acitivity use finish()
onclick of backbutton. If your on A activity then it closes the app. see below code.
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
}
return super.onKeyDown(keyCode, event);
}
Using android.os.Process.killProcess(android.os.Process.myPid());
which kills the app i.e force close the app & goto the home screen.see below code.
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// Kills & force closes the app
android.os.Process.killProcess(android.os.Process.myPid());
}
return super.onKeyDown(keyCode, event);
}