How to clear the previous activity stack and exit the application to the back button?

friends

I have three actions

A, B, C

A is the home screen.

Actions start as follows: A-> B-> C

If I come to the main screen using the stand, I want to clear the activity stack / previous history of actions, and it should exit the application.

will someone tell me how to do this?

+7
source share
4 answers

Very simple: use intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); for the intent used to trigger action A.

+13
source

You can do the following:
1. set clearTaskOnLaunch = "true" in AndroidManifest, in activity declaration A
2. in action C:

 @Override public void onBackPressed(){ moveTaskToBack(true); } 

therefore, if the user clicks back - he returns to the desktop if the user starts the application again - the task stack is cleared and he comes to root activity (A)

+2
source

In step A, try the following:

 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){ finish(); } return super.onKeyDown(keyCode, event); } 

This should ensure that if you click the back button, the activity will be finish() ed. If this action is at the bottom of the stack, finish should exit the action.

0
source

I think that compost is true, but if not, then from A you can start B with startActivityForResult () and in onActivityResult () process the received "message". Action B will send a CLOSE_ACTIVITY message if the back button has been pressed.

0
source

All Articles