在Android中从活动C到活动A时如何杀死活动B [英] how to kill Activity B when coming from Activity C to Activity A in android

查看:105
本文介绍了在Android中从活动C到活动A时如何杀死活动B的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android应用程序开发的初学者.我开发了一个具有5个活动的应用程序:(即活动-MainActivity, B, C, D, E).

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).

当用户打开应用程序时,将触发MainActivity并接受用户的输入.单击按钮后,用户将进入Activity B.

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.

Activity B中有3个按钮,可将用户带到3个不同的活动中:

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

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

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

进入这3个活动(即CDE)中的任何一个后,按下返回按钮,用户将被带回到活动B.来自用户的c8>,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.

如果这3个活动中均显示任何警报"对话框,则在对话框中单击"OK"后,将直接将用户带回到MainActivity以从用户那里获取新的输入.并且该循环再次继续.并且,如果在Activity B处于活动状态时用户按了向后按钮,则该用户将被带到MainActivity.

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.

当用户按下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.

但是发生的情况是当用户单击"yes"时,MainActivity正在完成,但是Activity B再次出现在顶部.就像它在堆栈中一样,在Activity B上按下后退按钮会将用户带到MainActivity.用户多次通过Activity B向后导航,当用户单击对话框中的"Yes"时,活动B就会出现很多次.

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.

我不知道如何通过在不影响活动之间导航的情况下完成活动来关闭应用程序,因为这些活动之间也存在大量数据流(主要是Activity B).我尝试使用Intent.FLAG_ACTIVITY_CLEAR_TASKIntent.FLAG_ACTIVITY_CLEAR_TOP,但是没有用.活动B仍在进行中.

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.

提前谢谢.

@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();
}

推荐答案

当您在活动C,D,E中单击OK时,这是要跳过Activity B的同时将您带到main activity,因此您应该指定标志

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

我认为应该可以.

如果实际上不起作用(我会感到惊讶),则可能的替代方法是在活动B中使用startActivityForResult(),您要向其中编写结果处理

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();
    }
}

在活动C中,单击确定按钮;

And in Activity C, when you click OK button;

this.setResult(1);
finish();

这篇关于在Android中从活动C到活动A时如何杀死活动B的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆