Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear previous activity stack and Exit Application on back button?

I have three activities:

A,B,C

A is home screen.

Activities are launched as follow A->B->C

If I come to home screen using backbutton, I want to clear activity stack/previous activities history and it should exit the application.

Can anyone guide me how to achieve this?

like image 332
UMAR-MOBITSOLUTIONS Avatar asked May 11 '11 13:05

UMAR-MOBITSOLUTIONS


2 Answers

Very simple: use intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); on an intent used to start the activity A.

like image 194
compostus Avatar answered Nov 02 '22 04:11

compostus


You can do following:
1. Set clearTaskOnLaunch = "true" in AndroidManifest, in declaration of activity A
2. In activity C:

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

So if user presses back - it comes back to homescreen.
If user launches application again - task stack clears and he comes to root activity (A)

like image 44
Arseniy Avatar answered Nov 02 '22 03:11

Arseniy