Android的活动与服务的关系 - 暂停后,后停止 [英] Android Activity and Service relationship - after Pause, after Stop

查看:99
本文介绍了Android的活动与服务的关系 - 暂停后,后停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设活动A被创建,然后A开始服务S和结合自身S.小号通知一个,这将导致A的状态改变的更新。 发生在A和S的Andr​​oid什么暂停或停止?

Let's say Activity A is created and then A starts a Service S and binds itself to S. S notifies A of updates which will cause the state of A to change. What happens to A and S after Android pauses or stops A?

例如:


  • 是否暂停自动从S(从而停止■如果是绑定到它的最后一件事?)
  • 解除绑定
  • 请从的事业一个更新将被收回?

  • 请选自S的更新永远不会传递给A和没有发生错误?

  • S能意识到,A被暂停或停止? (例如通过像 A.isAsleep的东西()的调用

  • Does pausing A automatically unbind it from S (thus stopping S if A is the last thing bound to it?)
  • Do updates from S cause A to be resumed?
  • Do updates from S never get passed to A and no error occurs?
  • Can S become aware that A is paused or stopped? (for instance by a call to something like A.isAsleep())

此外,如何处理这种情况,使正确的事情发生了什么?我在解除绑定在onPause(服务)并在onResume重新绑定()?

Also, How do I handle the situation so that the "right thing" happens? Do I unbind services in onPause() and re-bind upon onResume()?

推荐答案

(A)将是(S)的onPause未绑定的,但是如果你没有调用解除绑定的方法,我想你会得到一个内存泄漏。

(A) will be unbound from (S) onPause, but if you didn't call an unbind method, I think you will get a memory leak.

除非你重新绑定(A)至(S)onResume(),(A)会注意到意识到(S),除非活动是重新创建。

Unless you rebind (A) to (S) onResume(), (A) will note be aware of (S) unless the activity is recreated.

取决于你如何发送更新。 NPE的可以,如果你在所有参考(A)发生肯定与它发生暂停时被销毁。

Depends on how you are sending updates. NPE's can happen for sure if you at all reference (A) and it happens to be destroyed while paused.

(S)将只知道,如果你有这样的沟通,(S)为你做的方法(A)被暂停。

(S) will only be aware that (A) was paused if you have a method of communicating this to (S) as you have done.

是的,你必须恰到好处。你应该绑定和取消绑定onResume和的onPause。

And yes, you have it just right. You should bind and unbind onResume and onPause.

这篇关于Android的活动与服务的关系 - 暂停后,后停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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