In Android I have some activities, let\'s say A, B, C.
In A, I use this code to open B:
Intent intent = new Intent(this, B.class);
startActivity(inte
If your application has minimum sdk version 16 then you can use finishAffinity()
Finish this activity as well as all activities immediately below it in the current task that have the same affinity.
This is work for me In Top Payment screen remove all back-stack activits,
@Override
public void onBackPressed() {
finishAffinity();
startActivity(new Intent(PaymentDoneActivity.this,Home.class));
}
http://developer.android.com/reference/android/app/Activity.html#finishAffinity%28%29
Use finishAffinity() to clear all backstack with existing one.
Suppose, Activities A, B and C are in stack, and finishAffinity(); is called in Activity C,
- Activity B will be finished / removing from stack.
- Activity A will be finished / removing from stack.
- Activity C will finished / removing from stack.
I tried all solutions and none worked individually for me. My Solution is :
Declare Activity A
as SingleTop
by using [android:launchMode="singleTop"]
in Android manifest.
Now add the following flags while launching A
from anywhere. It will clear the stack.
Intent in = new Intent(mContext, A.class);
in.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK );
startActivity(in);
finish();
In manifest
android:launchMode="singleTask"
and
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
It sounds to me like you need to start Activity C
from Activity B
by using startActivityForResult()
. When you click a button in Activity C
, call setResult(RESULT_OK)
and finish()
so Activity C
is ended. In Activity B
, you could have the onActivityResult()
respond by also calling finish()
on itself, and you'd then be taken back to Activity A
.
In addition to FLAG_ACTIVITY_CLEAR_TOP
, you may try adding Intent.FLAG_ACTIVITY_SINGLE_TOP
as well:
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);