中的onCreate(),或在onResume绑定服务() [英] Binding to Service in onCreate() or in onResume()

查看:101
本文介绍了中的onCreate(),或在onResume绑定服务()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道什么是一个活动的最佳场所绑定到一个服务? 我看到的例子做在 onResume(),并在的onCreate()。我问自己,如果它不是一个问题,把它变成的onCreate(),因为的onPause()我会做一个解除绑定的服务,所以我没有得到一个serviceConnectionLeak,如果我离开了活动。现在,如果我preSS的主页按钮,然后切换到主屏幕,活动会从服务解除绑定,当我再回到从任务管理器,然后的onCreate()将不会被调用,如果code试图从服务访问功能,我会得到一个NullPointerException异常。如果我绑定和取消绑定仅在 onResume()的onPause()我没有这个问题。我说得对吧?

I want to know what is the best place in an Activity to bind to a service? I saw examples doing it in onResume() and also in onCreate(). I was asking myself if it is not a problem putting it into onCreate(), because in onPause() I will do a unbind to the service, so I don't get a serviceConnectionLeak, if I leave the activity. Now if I press the Home Button and then switch to the Home Screen, the Activity will unbind from the service, when I go back to the Activity from the Task Manager, then onCreate() will not be called and if the code is trying to access a function from the service I will get a NullPointerException. If I bind and unbind only in onResume() and onPause() I don't have this problem. Am i right?

推荐答案

我一般会建议做这个或者的onCreate() / 的onDestroy( ) ONSTART() / 的onStop(),这取决于你想要的语义

I would generally recommend doing this in either onCreate()/onDestroy() or onStart()/onStop(), depending on the semantics that you want:

  • 如果你的活动希望可与服务正在运行的整段时间进行交互(为例如,也许可以从你的网络检索的一些数据,并返回数据准备好时,你要允许这种情况发生,而在后台,所以如果用户返回您将有数据准备好),那么的onCreate() / 的onDestroy()可能是适当的。注意,这里的语义是,你的活动运行它所需要的服务的全部时间,所以如果这个服务在另一个进程中运行,那么你已经增加了它的重量,使它更容易为它被杀死,而在后台。

  • If your Activity wants to be interacting with the Service the entire time it is running (for example maybe it can retrieve some data from a network for you and will return the data when ready and you want to allow this to happen while in the background so if the user returns you will have the data ready), then onCreate()/onDestroy() is probably appropriate. Note that the semantics here is that the entire time your Activity is running it needs the Service, so if this Service is running in another process then you have increased the weight of it and made it more likely for it to be killed while in the background.

如果你的活动是唯一的兴趣与工作的服务同时可见,则 ONSTART() / 的onStop()为宜。这意味着你的活动将解除从服务当用户离开它(和它不再是可见的),并连接备份下次回报,这是重新启动和恢复。

If your Activity is only interested in working with the Service while visible, then onStart()/onStop() is appropriate. This means your Activity will unbind from the Service when the user leaves it (and it is no longer visible) and connect back up the next time the return and it is re-started and resumed.

我一般不建议做绑定/解除绑定在 onResume()的onPause()。这些一般不会显著减少使用服务的量(因此你的开销),而事实上,因为暂停和恢复发生在每一个活动的过渡,这是一个code路径要保持尽可能轻。这样做可以在这里有其他意想不到的不良后果:例如,如果多个活动在你的应用程序绑定到相同的服务 ,当两个这些活动的服务也被摧毁和重建作为当前活动之间的过渡是暂停下一个被恢复了。

I generally wouldn't recommend doing bind/unbind in onResume() and onPause(). These generally won't decrease significantly the amount you use the Service (and thus your overhead), and in fact, because a pause and resume happens at every activity transition, this is a code path you want to keep as lightweight as possible. Doing it here can have other unexpected negative consequences: for example if multiple Activitys in your app bind to the same Service, when there is a transition between two of those activities the Service may also get destroyed and recreated as the current Activity is paused before the next one is resumed.

此外,这些对(的onCreate() / 的onDestroy() ONSTART() / 的onStop()的onPause() / onResume( ))的本意是适当的对获取,然后释放资源(如绑定到服务 S,登记接收器等),以确保它们已正确被需要之前获取和释放(和不泄漏)时,不再需要。

Also these pairs (onCreate()/onDestroy(), onStart()/onStop(), onPause()/onResume()) are intended to be the proper pairs for acquiring and then releasing resources (such as binding to Services, registering receivers, etc) to ensure that they are correctly acquired prior to being needed and released (and not leaked) when no longer needed.

这篇关于中的onCreate(),或在onResume绑定服务()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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