Skip going back to direct parent activity when pre

2019-01-18 07:46发布

问题:

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!

回答1:

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!



回答2:

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.



回答3:

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.



回答4:

please set this FLAG before launching a new Activity

intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);



回答5:

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.



回答6:

In Manifest file mention in activity tag below value

android:noHistory="true"

then it will skip the activity



回答7:

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.