Communication with Android and the service - after a pause, after a stop

Suppose action A is created, and then A starts the service S and binds itself to S. S notifies A of updates that will lead to a change in state A. What happens to A and S after Android pauses or stops A?

For instance:

  • Does pause A automatically detach from S (thus stopping S if A is the last one associated with it?)
  • Can updates from S lead to renewal of A?
  • Can updates from S never be transmitted to and an error occurs?
  • Can S realize that A is paused or stopped? (e.g. by calling something like A.isAsleep() )

Also, How do I handle the situation so that the “right thing” happens? Disable services in onPause () and re-bind onResume ()?

+4
source share
1 answer

(A) will be disconnected from (S) onPause, but if you did not call the unbind method, I think you will get a memory leak.

If you do not return (A) to (S) onResume (), (A) notice that you know (S), unless the activity is recreated.

Depends on how you post updates. NPE can happen for sure if you reference (A) at all, and it can be destroyed during a pause.

(S) will only know that (A) is paused if you have a way of transmitting this message (S) as you did.

And yes, you are fine. You must bind and untie onResume and onPause.

+2
source

All Articles