完成所有的活动时,后退按钮是pressed [英] Finish all activities when back button is pressed

查看:108
本文介绍了完成所有的活动时,后退按钮是pressed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用程序,有3 activites。对于第一和第二活动我想后退按钮退出所有现有的活动。

I have an android application that has 3 activites. For the 1st and 2nd activity I want the back button to exit all existing activities.

目前的后退按钮退出它启动了,但如果pressed 2日活动,第一次活动将被显示出来,而不是退出应用程序的第一个活动导致到第二活动

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.

我需要这个功能是因为第一和第二活动中使用计时表计数器,继续运行在HOME键是presssed,我想原因。但是,我需要一种方法来复位定时器使用BACK按钮完全退出该应用程序。

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.

下面是我的$ C $下背部按钮,在present同时在第一和第二活动。

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

        }         
  };  

我已经研究使用下面的code的可能性:

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

不过,我不知道在哪里,这应该得到执行,我真的只是想,如果是BACK按钮后会显示确认信息后,选择结束所有的应用程序是pressed(见第$ C $Ç段)。

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

任何帮助在得到这个工作将是极大的AP preciated!

Any help in getting this working would be greatly appreciated!

感谢您

推荐答案

@Rob当你从活动1导航到2和2到3意图 使用完成功能

@Rob when you are navigating from activity 1 to 2 and 2 to 3 with intent use finish function

喜欢

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

这会杀了活动从中你会去到下一个活动  然后当你preSS的后退按钮它会带你的应用程序之外, 因为会有在堆栈没有活性

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

这篇关于完成所有的活动时,后退按钮是pressed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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