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 andr
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();
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 .
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);
As other answers have the following should work.
Intent intent = new Intent(Activity3.this, FinishActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
For API level < 11, Intent.FLAG_ACTIVITY_CLEAR_TASK
is not available. Instead I used IntentCompat.FLAG_ACTIVITY_CLEAR_TASK
from support library.
See IntentCompat
Might help someone stumbling across.
Try this:
Intent intent = new Intent(Activity3.this, FinishActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
Also you can use finishAffinity()
function in last activity like:
finishAffinity()
startHomeActivity()
Hope it'll be useful.