aswin akhilesh
aswin akhilesh

Reputation: 115

Skip going back to direct parent activity when pressed back

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. While in C, when I press BACK button, it should go back to A[Instead of the B by default]. And pressing BACK in A will exit the program.

I tried to call an intent from C to A. But in this case the call invocation gets into a loop : A -> B -> C -> A since the new activity is pushed on top of the stack. As a result, when BACK is pressed at A, instead of exiting [A is the start], it goes to C and then B and then back to A in a needless circle.

It would be great if someone could give a better way to address this loopy scenario!

Upvotes: 6

Views: 9532

Answers (8)

O Thạnh Ldt
O Thạnh Ldt

Reputation: 1223

  • 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;

Upvotes: 0

Harish
Harish

Reputation: 237

In Manifest file mention in activity tag below value

android:noHistory="true"

then it will skip the activity

Upvotes: 1

Jess
Jess

Reputation: 211

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.

Upvotes: 12

Bharat Sharma
Bharat Sharma

Reputation: 3966

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.

Upvotes: 0

Lalit Poptani
Lalit Poptani

Reputation: 67286

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.

Upvotes: 1

Manjunath
Manjunath

Reputation: 2063

please set this FLAG before launching a new Activity

intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

Upvotes: 1

nithinreddy
nithinreddy

Reputation: 6197

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!

Upvotes: 21

Niko
Niko

Reputation: 8153

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.

Upvotes: 2

Related Questions