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
Give this a try. This should clear your activity stack.
Try this:
From documentation for
Intent.FLAG_ACTIVITY_CLEAR_TOP
So to get it work your
FinishActivity
must be first one in your Activity stack. In any other cases this solution wouldn't give you anything.You must perform several steps to perform this:
1) Make
FinishActivity
as your launcher activity.2) Do not provide any view for it and start first
Activity
of your application directly fromonCreate
callback :3) Redefine
onRestart
callback:Code sample:
I guess this is all you need. Good luck!
This worked for me:
Also you can use
finishAffinity()
function in last activity like:Hope it'll be useful.
setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TOP);
apart from that set launchMode to "singleTop" in FinishActivity definition in xml, overwrite onNewIntent method , you can pass some additional information as part of intent , instated of finishing your activity in onCreate finish it in onNewIntent method of activity based on some signal from calling activity or simply finish it based on your need . It maybe possible your other activities have different lauchmodes that's why they are not finishing .