Android - 按住主键时关闭应用程序 [英] Android - Close app when home key is pressed

查看:107
本文介绍了Android - 按住主键时关闭应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使这个应用程序工作正常,但是当我离开应用程序并再次打开它时,它总是强制关闭。如果用户按住家里的还是后退键,它会如何处理呢?

I made this app that works fine, but when i leave the app and open it again it always force closes. how can i make it so when the user presses the home and or back key it will kill the app process?

当应用程序再次打开时,它有一个新鲜的开始。

Making it so when the app is opened again it has a fresh start.

推荐答案

你不应该杀死进程。您应该处理主页按钮和其他此类交互,通过 onPause() onResume()方法

You are not supposed to "kill the process". You should handle home button presses and other such interactions that navigate away from your app via the onPause() and onResume() methods.

在您的 onPause()方法中,您应该保存应用程序的状态。这可以从保存在Bundles,SharedPreferences,sqlite或任何形式的持久性适用于您的应用程序。

In your onPause() method, you should save the state of your app. This can range from saved it in Bundles, SharedPreferences, sqlite or whatever form of persistence is appropriate for your app.

在您的 onResume()方法,你应该恢复你的应用程序的状态,这样一来,用户就没有任何改变的错觉。

In your onResume() method, you should restore the state of your app so that the user is given an illusion that nothing has changed at all.

处理多任务,这是您应该如何适应您的应用程序,以及有效和可靠地适合您的应用程序到Android框架。

This is how android handles multitasking and this is how you must accommodate it within your app as well to efficiently and reliable suit your app to the android framework.

我建议你通过活动生活循环指南在这里:

I suggest you go through the activity life cycles guide here first:

http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

更多关于暂停和恢复活动具体来说:

More on pausing and resuming an activity specifically here:

HTTP://德veloper.android.com/training/basics/activity-lifecycle/pausing.html

此外,您可能希望在这里阅读数据存储选项:

Also, you might want to read up on data storage options here:

http: //developer.android.com/guide/topics/data/data-storage.html

这篇关于Android - 按住主键时关闭应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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