how to kill Activity B when coming from Activity C

2019-09-16 12:13发布

I'm a beginner in android app development. I've developed an app which has 5 Activities: (i.e. Activity - MainActivity, B, C, D, E).

when the user opens the app the MainActivity will firedm and takes inputs from the user. Upon clicking a button, the user will taken to Activity B.

There are 3 buttons in Activity B which takes user to 3 different activities:

(i.e. button-1 --> Activity C, button-2 --> Activity D, Button-3 --> Activity E).

after going into any of these 3 Activities (i.e. C,D,E), on pressing back button, the user will be taken back to Activity B. and whichever Activity among C,D,E the user came from will be finished.

If any Alert dialog is displayed in these 3 Activities, upon clicking on "OK" in the dialog, the user will be taken back directly to MainActivity to take fresh inputs from the user. and the cycle continues again.and also if the user pressed back button when Activity B is active, then the user will be taken to MainActivity.

when the user press back button in MainActivity, a dialog box appears asking if the user wants to exit from the app. upon clicking on "Yes" the app should finish all the activities and close the app.

but what happening is when the user click on "yes" the MainActivity is finishing but the Activity B is appearing on top again. as it is in the stack, and pressing back button when on Activity B will take user to MainActivity. a number of times the user navigates back n forth through Activity B, that many times the Activity B is coming up when the User clicks on "Yes" in the dialog box.

I don't know how to close the app by finishing the activities without affecting the navigation between the activities as there is also a flow of data among those activities (mainly Activity B). I've tried using Intent.FLAG_ACTIVITY_CLEAR_TASK, Intent.FLAG_ACTIVITY_CLEAR_TOP but it didn't work. The Activity B is still coming up.

Please help me resolve this problem without interrupting the navigation or data flow.

thanks in advance.

EDIT:

@Override public void onBackPressed() {
            new AlertDialog.Builder(this).setMessage("Are you sure, you want to exit?")
            .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    finish();
                }
            })
            .setNegativeButton("No", null)
            .show();
}

3条回答
Lonely孤独者°
2楼-- · 2019-09-16 12:19

If you want to clear all activities you can use finishAffinity() if you are targeting 16 and 16+. For below 16 flags

Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP

Can be used. The finishAffinity() will finish all the activity no matter how many are there below in the task stack. if you don't want to have an Activity visible if you comeback from another Activity you can finish it when you start the another.

查看更多
三岁会撩人
3楼-- · 2019-09-16 12:28

When you click OK in Activity C,D,E which is meant to take you to the main activity while skipping Activity B, then you should specify flag

Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

I think that should work.

If that actually doesn't work (I'd be surprised), a possible alternative is using startActivityForResult() in Activity B, to which you'd write a result handling

Intent intent = new Intent(this, ActivityC.class);
startActivityForResult(intent, 1);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == 1 && resultCode == 1) {
        this.finish();
    }
}

And in Activity C, when you click OK button;

this.setResult(1);
finish();
查看更多
你好瞎i
4楼-- · 2019-09-16 12:41

you may use: Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP. (For Example)

Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);

To clear all previous activitys(s) and start a new one.

and check if exit=true finish() the current one.

you can also use: android:noHistory="true" in your activity Tag in the Manifest.xml

Note: this will keep no history it means that after you open another activity the previous one will be killed.

查看更多
登录 后发表回答