hcp
hcp

Reputation: 279

How to Clear the Stack and close the application

I want clear the stack and close the application by pressing on Exit Button in my application.So that when ever I open the application again i need to start the application from first activity.

I am using below statements but these statements are not working.I don't want to use Launch mode Single Task.

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Upvotes: 1

Views: 1834

Answers (1)

CRUSADER
CRUSADER

Reputation: 5472

Closing all the previous activities as follows:

Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("Exit me", true);
startActivity(intent);
finish();

Then in MainActivity onCreate() method add this to finish the MainActivity

if( getIntent().getBooleanExtra("Exit me", false)){
    finish();
}

Also check out this link

Upvotes: 4

Related Questions