努力实现我的Andr​​oid应用程序的退出确认 [英] Trying to Implement an Exit Confirmation in my Android app

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

问题描述

我已经发现了,你不能覆盖在Android手机上的主页按钮。它退出应用程序,它总是退出应用程序,它不与任何一种娇媚确认的麻烦。我想我明白谷歌的理由 - 但我认为这是一个有点短视...

I've already discovered that you can't override the Home button on an Android phone. It exits the application, it ALWAYS exits the application, and it DOESN'T bother with any sort of namby-pamby confirmation. I suppose I understand Google's reasoning -- but I do think it's a bit short-sighted...

反正(我之前了解了Home键),我建立了我的应用程序,使用户可以通过退出选项菜单中的应用 - 使用onCreateOptionsMenu()和一个XML文件,我设置了一个简单的弹出时显示在菜单按钮pssed $ p $菜单。它的一个选择是退出,它工作正常。

Anyway, (before I learned about the Home button), I set up my app so the user can exit the application through the Options Menu -- using onCreateOptionsMenu() and an XML file, I set up a simple pop-up menu that's displayed when the Menu button is pressed. One of its choices is Exit, and it works fine.

然而,它发生,我认为这可能是很好的做法,以一个确认对话框添加到出口过程中(即使它也可以被认为是多余的)。所以,我创建了一个AlertDialog标题为你想退出吗?并且是和否按钮...

However, it occurred to me that it might be good practice to add a confirmation dialog to the exit process (even if it could also be considered superfluous). So, I created an AlertDialog with the title "Do you want to Exit?" and Yes and No buttons...

对于按钮的点击监听器很简单,只需设置exitConfirm(一个布尔值)true或false。在code处理然后退出菜单的选择,我的应用程序后,清理并执行完成()或不依赖于exitConfirm ...

The click listeners for the buttons are simple and just set exitConfirm (a boolean) true or false. The code that handles the Exit menu choice then cleans up after my application and executes finish() or not depending on the state of exitConfirm...

不幸的是,它完全不工作...所有在onOptionsItemSelected()的退出情况下,code的执行,然后显示对话框!我想我应该已经看到了未来。而且我想,如果我继续冲击它,我会想出一个办法做到这一点,但我想我会问社会各界的建议 - 所以,没有任何人有一种方式的建议顺利退出的Andr​​oid应用程序方式包括从用户??

Unfortunately, it completely doesn't work... All of the code in onOptionsItemSelected() for the exit case executes and THEN the Dialog is displayed!! I suppose I should've seen that coming. And I suppose if I keep pounding on it, I'll come up with a way accomplish this, but I thought I would ask the community for suggestions - so, does anybody have a suggestion for a way to smoothly exit an Android application in a manner that includes the step of getting confirmation from the user??

谢谢,
R上。

Thanks, R.

推荐答案

显然,它通常被认为不是很酷在你的Andr​​oid应用程序的退出按钮(或菜单项,或其他)。显然,这是因为你的应用程序并没有真正退出。这只是最小化出的方式,和presumably,操作系统会照顾它最终。

Apparently, it's generally considered not to be cool to have an exit button (or menu item, or whatever) in your Android app. Apparently, this is because your application doesn't really exit. It's only minimized out of the way, and presumably, the OS will take care of it eventually.

不过,如果你在做什么,需要的时候,你就大功告成了做清理一定程度上,这是我想我知道的:

However, if what you're doing requires some level of clean-up when you're done doing it, this is what I think I know about that:


  1. 仔细注意各种Android的生命周期方法,尤其是踢什么他们。生命周期方法包括的onCreate(),在onStart()onResume(),在onPause(),和的onStop()。还有的onDestroy() - 该补充的onCreate() - 但我目前不使用它

  1. Pay careful attention to the various Android "Life Cycle Methods", especially what kicks them off. Life cycle methods include onCreate(), onStart(), onResume(), onPause(), and onStop(). There's also onDestroy() -- the complement to onCreate() -- but I don't currently use it.

这些方法称为响应各种事件(如pressing Home键),但是在我的情况下,重要的是,最大限度地减少应用程序调用的onPause(),并最大限度地发挥它调用onResume()。因此,我需要调用我的设置和从这些位置推倒方法,而不是,例如,从的onCreate()和的onDestroy()。

These methods are called in response to various events (like pressing the Home button), but what was important in my case was that minimizing the application calls onPause() and maximizing it calls onResume(). Hence, I needed to call my set up and tear down methods from those locations, and NOT, for instance, from onCreate() and onDestroy().

有是我用遍了我的code用一个函数调用完成()。现在,它只是在被称为处理通讯中断我的外部设备的方法。我相信,在调用完成()的结果是完全pssing Home键的$ P $的结果 - 所以,这只是一种preSS,在软件的主页按钮

There is a function called finish() that I used to use all over my code. Now it's only in the method that's called to handle loss of communication with my external device. I believe the results of calling finish() are exactly the results of pressing the Home button -- so, it's just a way to "press" the Home button in software.

啰嗦结论这一切是Home按键绝对不会隐藏用户的角度审视自己的应用程序。你不能捕获此键preSS - 不管你想有多严重添加某种你确定?功能。然而,应用程序的最小化,不是封闭的,这可以是好还是坏。如果你写你的启动和正常关机code(并将其添加到适当的生命周期方法),你会不会吹的东西了,你的用户将能够轻松地返回到你的应用程序,一旦他们也学长preSS Home键,看看最小的应用程序列表。

The long-winded conclusion to all this is the Home button absolutely WILL hide your application from the user's view. You cannot trap this key press -- no matter how badly you want to add some sort of, "Are you sure??" functionality. However, the app's been minimized, not closed, and this can either be good or bad. If you've written your start up and shut down code properly (and added it to the proper life cycle methods), you won't blow things up, and your user will be able to easily return to your app once they've learned to long press the Home button to see a list of minimized apps.

运气好的话,我想,他们会责怪谷歌他们的挫折,而不是你......

With any luck, I suppose, they'll blame their frustration on Google, and not on you...

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

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