简体   繁体   English

在Android中从Activity C到Activity A时如何杀死Activity B

[英]how to kill Activity B when coming from Activity C to Activity A in android

I'm a beginner in android app development. 我是android应用程序开发的初学者。 I've developed an app which has 5 Activities: (ie Activity - MainActivity, B, C, D, E ). 我开发了一个具有5个活动的应用程序:(即活动MainActivity, B, C, D, E )。

when the user opens the app the MainActivity will firedm and takes inputs from the user. 当用户打开应用程序时, MainActivity将触发并接受用户输入。 Upon clicking a button, the user will taken to Activity B . 单击按钮后,用户将进入Activity B

There are 3 buttons in Activity B which takes user to 3 different activities: Activity B有3个按钮,可将用户带到3个不同的活动中:

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

after going into any of these 3 Activities (ie 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. 进入这三个活动(即CDE )中的任何一个之后,按返回按钮,将使用户返回到活动B。并且,来自用户的CDE的哪个活动都将完成。

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

when the user press back button in MainActivity , a dialog box appears asking if the user wants to exit from the app. 当用户在MainActivity按下“后退”按钮时,将出现一个对话框,询问用户是否要退出该应用程序。 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. 但是发生的是,当用户单击"yes" ,MainActivity正在完成,而Activity B又出现在顶部。 as it is in the stack, and pressing back button when on Activity B will take user to MainActivity . 就像它在堆栈中一样,在Activity B上按下返回按钮会将用户带到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向后导航的次数,即当用户在对话框中单击"Yes"时,活动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 ). 我不知道如何在不影响活动之间的导航的情况下完成活动来关闭应用程序,因为这些活动(主要是Activity B )之间也存在数据流。 I've tried using Intent.FLAG_ACTIVITY_CLEAR_TASK , Intent.FLAG_ACTIVITY_CLEAR_TOP but it didn't work. 我尝试使用Intent.FLAG_ACTIVITY_CLEAR_TASKIntent.FLAG_ACTIVITY_CLEAR_TOP但是它没有用。 The Activity B is still coming up. 活动B仍在进行中。

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

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 当您在活动C,D,E中单击OK (这是要使您跳到Activity B同时转到main activity时,则应指定标志

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 如果这实际上不起作用(我会感到惊讶),则可能的替代方法是在Activity B使用startActivityForResult() ,向其编写结果处理

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; 在活动C中,当您单击“确定”按钮时;

this.setResult(1);
finish();

you may use: Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP 您可以使用: Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP . 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. 并检查exit=true finish()当前值。

you can also use: android:noHistory="true" in your activity Tag in the Manifest.xml 您还可以在Manifest.xml的活动标签中使用: android:noHistory="true"

Note: this will keep no history it means that after you open another activity the previous one will be killed. 注意:这将不会保留任何历史记录,这意味着您打开另一个活动后,上一个活动将被杀死。

If you want to clear all activities you can use finishAffinity() if you are targeting 16 and 16+. 如果您想清除所有活动,并且目标是16和16+,则可以使用finishAffinity() For below 16 flags 16个以下标志

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. finishAffinity()将完成所有活动,无论任务堆栈中下面有多少活动。 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. 如果您不想看到一个Activity如果您从另一个Activity回来),则可以在启动另一个Activity时将其完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM