Android的 - 当home键是pressed关闭应用程序 [英] Android - Close app when home key is pressed

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

问题描述

我做了这个应用程序,工作正常,但是当我离开应用程序,然后再次打开它,它总是强制关闭。我怎样才能使它所以当用户presses家庭和或返回键,将终止该应用程序?

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.

推荐答案

您不应该杀进程。你应该处理home键presses等这样的互动,通过的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()方法,你应该保存您的应用程序的状态。这可以从保存的捆绑它,共享preferences,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.

这是怎样的Andr​​oid手柄多任务,这是多么您必须在应用程序内适应它,以及高效,可靠地满足您的应用程序到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:

<一个href=\"http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle\">http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

更多关于暂停并专门在这里恢复的活动:

More on pausing and resuming an activity specifically here:

<一个href=\"http://developer.android.com/training/basics/activity-lifecycle/pausing.html\">http://developer.android.com/training/basics/activity-lifecycle/pausing.html

此外,您可能想在这里数据存储选项念起来:

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

<一个href=\"http://developer.android.com/guide/topics/data/data-storage.html\">http://developer.android.com/guide/topics/data/data-storage.html

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

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