退出的Andr​​oid应用程序在后退按钮 [英] Exiting an Android Application On back button

查看:101
本文介绍了退出的Andr​​oid应用程序在后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我有不同的活动的应用程序。在1活动,我想,当用户presses后退按钮,我希望应用程序被关闭,则显示主屏幕

I have an application in which I have different activities. In 1 activity, I want that when the user presses the back button, I want the application to be closed and home screen is displayed

code

public boolean onKeyDown(int keyCode, KeyEvent event) 
        {
            //Handle the back button
            if(keyCode == KeyEvent.KEYCODE_BACK) 
                {
                    //Ask the user if they want to quit
                    new AlertDialog.Builder(this)
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .setTitle("Exit")
                    .setMessage("Really Exit ?")
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() 
                        {
                            public void onClick(DialogInterface dialog, int which) 
                                {
                                 Intent intent = new Intent(Intent.ACTION_MAIN);
                                intent.addCategory(Intent.CATEGORY_HOME);
                                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                startActivity(intent);
                                }
                        })
                    .setNegativeButton("No", null)
                    .show();
                    return true;
                }
            else 
                {
                return super.onKeyDown(keyCode, event);
                }
        }

当我运行这一点,preSS返回按钮,将显示主屏幕,但是当我运行它第二次,我得到的是

When I run this and press the back button, the home screen is displayed, but when I run it for the second time, I get a console output as

ActivityManager:警告:活动尚未开始,目前的任务已经放到了前面,

ActivityManager: Warning: Activity not started, its current task has been brought to the front.

和在其中的返回按钮是pressed获取显示的活性。 我认为,应用程序不会被打死,在后台运行。刚提,这不是我的应用程序的启动活动。

And the activity in which the back button is pressed gets displayed. I think the application does not get killed and runs in background. Just to mention, this is not the starting activity of my application.

有人可以帮我,我是初学者。

Can somebody help me, I am beginner.

推荐答案

对于每个意图您已经使用了进入其他活动u必须遵循这种方式传递的意图只是通过<强>标志以每个意图下面给出以及启动后的活动使用的 startActivity() u必须添加完成()之后演示code下面给出

For each and every Intent you have used for going into other activity u have to follow this way for passing intent just pass flag to each intent as given below and after starting Activity using startActivity() u have to add finish() after that demo code as given below

Intent i=new Intent(firstActivity.this,secondActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();

这篇关于退出的Andr​​oid应用程序在后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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