I have got a small problem in an Android app I am working on :
There are 3 activities namely A , B , C and the invocation is in the following order : A -> B -> C.
Very simple!! When you are starting the activity C, from B, use B.finish(). Something like this.
Intent i = new Intent(B.this, C.class);
B.this.finish();
startActivity(i);
This will remove B from the stack!
Set a flag for B activity like this
private boolean mDestroyActivity = false;
set that flag true when you call startActivity C.
for activity B onStop method add checking like this:
if (mDestroyActivity) finish();
Then when you press back button in C you will jump back to A.
I think you should not break the normal flow. C should first return to B then A.
Any way i am having solution of your problem. Register a broadcast receiver in A activity then from C send a broadcast to A. In A activity you can receive that broadcast and then clear the stack. This will automatically finish all child activities.
Override onbackpress and don't call super in onbackpress instead send a broadcast to activity A.
In activity B:
public static boolean mDestroyActivity = false; @Override protected void onResume() { super.onResume(); /** * exit when back from Activity C-> A (skip activity B) */ Log.i("FINISH", "= " + mDestroyActivity); if (mDestroyActivity) { mDestroyActivity = false; finish(); } }
In activity C:
B.mDestroyActivity = true;
Probably late, but for people who might find this in a search: You can add
android:noHistory="true"
to your activity B in your AndroidManifest. This will also avoid that the onActivityResult() method is called in activity B when C returns a result though. I'ts basically like B disappears as soon as you start C.
You can start Activity C with startActivityForResult()
and inside onActivityResult()
finish Activity B.
To start Activity C,
Intent intent = new Intent(ActivityB.this, ActivityC.class);
startActivityForResult(intent, 123);
And override in Activity B
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 123){
if(resultCode == Activity.RESULT_OK){
finish();
}
}
}
And inside Activity C use setResult(Activity.RESULT_OK)
before finish();
UPDATE:
Another way is to use FLAG_ACTIVITY_NEW_TASK
and FLAG_ACTIVITY_CLEAR_TOP
to start Activity A from Activity C.
Intent intent = new Intent(ActivitC.this, ActivityA.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
One more way can be just finish()
Activity B when you are starting Activity C. So, when you press back on Activity C it will directly move to Activity A as Activity B has already finished.