简体   繁体   English

按下后退按钮时完成所有活动

[英]Finish all activities when back button is pressed

I have an android application that has 3 activites.我有一个 android 应用程序,它有 3 个活动。 For the 1st and 2nd activity I want the back button to exit all existing activities.对于第一个和第二个活动,我希望后退按钮退出所有现有活动。

At the moment the back button is exiting the activity it is initiated on but if its pressed on the 2nd activity, the 1st activity will be displayed, rather than exiting the application as the 1st activity leads on to the 2nd.目前后退按钮正在退出启动它的活动,但如果在第二个活动上按下它,将显示第一个活动,而不是退出应用程序,因为第一个活动会导致第二个活动。

The reason I require this functionality is because the 1st and 2nd activities use chronometer timers which continue to run when the HOME button is presssed which I want.我需要此功能的原因是因为第一个和第二个活动使用天文钟计时器,当按下我想要的主页按钮时,它们会继续运行。 But I need a way to reset the timers to fully exit the application using the BACK button.但我需要一种方法来重置计时器以使用“后退”按钮完全退出应用程序。

Here is my code for the back button which in present in both the 1st and 2nd activities.这是我的后退按钮代码,它出现在第一个和第二个活动中。

@Override
 public void onBackPressed() { // method for exit confirmation
  AlertDialog.Builder builder = new AlertDialog.Builder(BreakActivity.this);
  builder.setMessage("Are you sure you want to exit?")
         .setCancelable(false)
         .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int id) {
                  BreakActivity.this.finish();
             }
         })
         .setNegativeButton("No", new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int id) {
                  dialog.cancel();
             }
         });
  AlertDialog alert = builder.create();
  alert.show();

        }         
  };  

I have researched the possibility of using the following code:我研究了使用以下代码的可能性:

intent = new Intent(this, FinActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent)
finish();

However, I'm not sure where this should be implemented and I really only want to end all the applications if 'Yes' is selected after the confirmation message is displayed after the BACK button is pressed (see first code snippet).但是,我不确定应该在哪里实施,如果在按下 BACK 按钮后显示确认消息后选择“是”,我真的只想结束所有应用程序(请参阅第一个代码片段)。

Any help in getting this working would be greatly appreciated!将不胜感激为使这项工作提供任何帮助!

Thank you谢谢

@Rob when you are navigating from activity 1 to 2 and 2 to 3 with intent use finish function @Rob 当您从活动 1 导航到 2 和 2 到 3 并有意使用完成时 function

like

 i=new Intent(Main_Menu.this, ABC.class);
          finish();
         startActivity(i);

It will kill the activity from which you will go to next activity and then when you press the backbutton it will take you outside the application, because there will be no activity in the stack它会杀死你将从 go 到下一个活动的活动,然后当你按下后退按钮时,它会将你带出应用程序,因为堆栈中没有活动

you can use this你可以用这个

        finish();
        Intent intent = new Intent(Intent.ACTION_MAIN); 
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        intent.addCategory(Intent.CATEGORY_HOME); 
        startActivity(intent);

note one thing always avoid system.exit to quit from application it is not recomended and may caus various problems请注意一件事总是避免 system.exit 从应用程序中退出,这是不推荐的,可能会导致各种问题

https://groups.google.com/forum/?fromgroups#!topic/android-developers/Y96KnN_6RqM https://groups.google.com/forum/?fromgroups#!topic/android-developers/Y96KnN_6RqM

Close application and launch home screen on Android 关闭应用程序并在 Android 上启动主屏幕

Use this line of code:使用这行代码:

System.exit(1);

//The integer 1 is just a return code.

how to close whole application in android 如何在 android 中关闭整个应用程序

You can use-您可以使用-

Process.killProcess(android.os.Process.myPid());

Don't call any System.exit or something else.不要调用任何 System.exit 或其他东西。 Android architecture is built to self managment of processes. Android 架构是为流程的自我管理而构建的。 Android itself decides when to kill app process. Android 本身决定何时终止应用程序进程。 When you killing your application by hand you are creating additional overhead for user device.当您手动终止您的应用程序时,您正在为用户设备创建额外的开销。

Try to manually reset your timers and revise your app architecture.尝试手动重置您的计时器并修改您的应用架构。

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

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