如何在Android应用关闭或失去焦点时调用方法? [英] How to call a method when an Android app is closed or loses focus?

查看:340
本文介绍了如何在Android应用关闭或失去焦点时调用方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为我正在构建的应用程序将处理相当敏感的数据,所以我希望每次用户登录时都将SQLite数据库与服务器同步,并在每次应用程序失去焦点时删除emty数据库(因为用户移动到主屏幕或其他应用程序)。

Because an app I'm building will handle rather sensitive data I want to sync the SQLite db with the server every time the user logs in, and remove emty the db every time the app loses focus (because the user moves to the home screen or another app).

查看活动生命周期,我的想法是通过清空每个Activity的onDestroy中的数据库来做到这一点。为了测试所描述的生命周期,我只是覆盖所有生命周期方法(onCreate,onStart,onResume,onPause,onStop和onDestroy),在其中包含一些日志消息,并启动我的应用程序。

Seeing the Activity lifecycle, my idea was to do this by emptying the database in the onDestroy of every Activity. To test the described lifecycle I just Override all lifecycle methods (onCreate, onStart, onResume, onPause, onStop, and onDestroy), included some log messages in them, and fired up my app.

日志消息包含在我的SettingsActivity中。当我进入我的应用程序并移动到设置时,它运行onCreate,onStart和onResume(如预期的那样)。然后,当我单击一个设置并移动到下一个屏幕时,它将在onPause和onStop上运行(仍然按预期方式)。要返回到设置屏幕,我单击后退按钮,它再次运行onStart和onResume(仍然按预期),当我现在再次单击后退按钮以返回到初始屏幕时,它(令我惊讶的是)运行onPause, onStop AND onDestroy。

The log messages are included in my SettingsActivity. When I enter my app and move to the Settings it runs onCreate, onStart and onResume (as expected). When I then click a setting and move to the next screen it runs onPause and onStop (still as expected). To move back to the settings screen I click the back button and it runs onStart and onResume again (still as expected), when I now click the back button again to move back to the initial screen, it (to my surprise) runs onPause, onStop AND onDestroy.

所以我的问题;


  1. 为什么在应用程序未完成时会破坏活动?

  2. 和更重要的是:当应用程序关闭或失去焦点时,如何运行我的CleanUp方法?

  1. Why does it destroy the Activity when the app is not finished?
  2. And more importantly: how can I run my CleanUp method when the app closes or loses focus?


推荐答案

您可以在此处获得更多信息: http:/ /developer.android.com/training/basics/activity-lifecycle/stopping.html

You can have some more information here : http://developer.android.com/training/basics/activity-lifecycle/stopping.html

即使我认为你已经阅读过,因为你已经研究过活动生命周期,你可以在第一个数字中看到在$ code> onStop()之后调用 onDestroy()呼叫完全由系统管理:你不应该期待任何行为。系统将自行决定何时调用此方法,有时,此方法永远不会被调用(请参阅此处: http://developer.android.com/reference/android/app/Activity.html )。当系统需要内存时,您的活动将传递 onStop()而不再是。

Even if I think you already read it because you already study the activity lifecycle, you can see in the first figure that the onDestroy() is called after the onStop() and this call is totally managed by the system : you shouldn't expect any behavior. The system will decide itself WHEN to call this method, and sometimes, this method will never be called (see here : http://developer.android.com/reference/android/app/Activity.html). When system needs memory, your activity will pass in onStop() and nothing more.

所以,回答你的问题第二个问题,你shloud阅读文档中有关 onDestroy()方法的说明:

So, to answer your second question, you shloud read the note in the documentation about the onDestroy() method :


注意:不要指望此方法被称为保存
数据的地方!
例如,如果某个活动正在内容
提供商中编辑数据,那么编辑应该在onPause()或
onSaveInstanceState(Bundle)中提交,而不是在这里。 此方法通常是
,用于释放与
活动相关联的资源(如线程
),以便被破坏的活动不会留下这些东西
,而其余的其应用程序仍在运行。有
的情况,系统将简单地杀死活动的托管
进程而不调用其中的此方法(或任何其他方法),因此
不应用于执行旨在执行的操作在此过程消失后,保持在
左右。

Note: do not count on this method being called as a place for saving data! For example, if an activity is editing data in a content provider, those edits should be committed in either onPause() or onSaveInstanceState(Bundle), not here. This method is usually implemented to free resources like threads that are associated with an activity, so that a destroyed activity does not leave such things around while the rest of its application is still running. There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away.

所以很明显,这是一个不好的地方让你清理处理。所以你要使用 onPause() onStop()方法之一。

So it's pretty clear that it's a bad place to make your clean-up process. So you shloud use one of onPause() or onStop() method.

onStop()在文档中描述如下:


当用户不再可见时调用。您接下来将收到 onRestart() onDestroy(),或者什么也不接受,具体取决于以后的用户活动。

Called when you are no longer visible to the user. You will next receive either onRestart(), onDestroy(), or nothing, depending on later user activity.

onPause()在文档中描述如下:


当一个活动进入后台但是尚未被杀死时,被称为活动生命周期的一部分。
[...]
当活动B在活动A前面启动时,此回调将在A上调用
在A的onPause()返回之前不会创建B. ,所以
肯定不会在这里做任何冗长的事情。

[...]
在系统需要更多内存的情况下,它可能会导致暂停的进程被重新开启资源。

我们现在知道 onPause()是旨在允许您保存数据,并且在 onPause()执行后,系统可以终止您的进程。因此,在 onPause()中进行清理似乎是最安全的地方,因为您非常确定每次都会调用它。

We now know that onPause() is designed to allow you to save your data, and also that after onPause() was executed, the system could kill your process. So, making the clean-up in onPause() seems to be the safest place as you're pretty sure it will be called everytime.

另外,正如你可以阅读的那样,在这里进行清理可能会使你的应用程序变慢,但无论如何在每次获得/松散焦点时清理和重新创建数据库都是一个非常繁重的过程...

Also, as you can read, making your clean up here can make your app slow, but anyway cleaning and recreating your database at each gain/loose of focus is a really heavy process...

要恢复:在 onPause()方法中进行清理过程,并在的onResume()。 请注意,使用此类流程时,您的应用程序可能会非常慢。

To resume : make your clean up process in the onPause() method and your init process in the onResume(). Keep it mind that your application can be really slow with this kind of process.

希望这可以帮助您。

这篇关于如何在Android应用关闭或失去焦点时调用方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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