Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intent.FLAG_ACTIVITY_CLEAR_TOP doesn't deletes the activity stack

I am developing the application in which i want to close whole application on button click. I know in android we should not think about to close the application because android does that automatically from this Is quitting an application frowned upon?. but yet i want to close my application.

So what i am doing to close application is i am using Intent.FLAG_ACTIVITY_CLEAR_TOP flag to delete the activity stack.

Intent intent = new Intent(Activity3.this, FinishActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();

And in onCreate of FinishActivity.class i am calling this.finish() but application is not get closed and previous activity gets reopened.

FinishActivity.class

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    this.finish();
}

Update :

Here is the scenario

MainActivity->Activity2->Activity3->FinishActivity

Here Activity2 is gets opened after finishing the activity.

How do i achieve this? Any idea and suggestion will be appreciated.

Thanks & Regards

like image 606
Juned Avatar asked Jan 08 '13 06:01

Juned


Video Answer


2 Answers

Give this a try. This should clear your activity stack.

Intent i = new Intent(this,MyActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
like image 71
IssacZH. Avatar answered Oct 17 '22 22:10

IssacZH.


This finally worked for me 100% of the time. I tried all of the flags standalone, never worked for all instances of the code. Used all the flags everywhere I want this functionality, about 5 different places, and it now works.

            Intent intent = new Intent(actvSignIn.this, actvNearbyPlaces.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
            finish();
like image 27
kennedy484 Avatar answered Oct 17 '22 23:10

kennedy484