Reputation: 3619
I am creating a program based on Android API version 7 and want to know the best way to handle communication between my main Activity
and my background worker thread.
I saw that there is a Handler
class that accepts Message
s however I have a doubt which I have researched and am still unsure of.
If the Activity
that is receiving the Message
s gets paused or even stopped, what happens to the background service when Message
s are pushed through the Handler
? Are they queued behind the scenes and delivered when the Activity
is resumed? Lastly, similar to Content Provider
s, does the Activity
somehow have to unregister its handler when paused? What consequences are there posting information to a paused Activity
?
I am not set on any single way of handling this yet, so please offer your suggestions for the best way to handle this kind of communication. I need to listen for updates coming from this background thread and act upon them serially (in order) whenever they are received to the front end.
I have seen posts such as this here: Best way for Service that starts Activity to communicate with it
However these don't really properly answer my question, so I felt it necessary to start a new post
There are also are also AsyncTask
s but the documentation doesn't really say how to handle paused Activity
s
Upvotes: 0
Views: 805
Reputation: 1854
When your activity is paused and you want to send a message to it, you should use Intent, but with Inten, your activity will be started again. If you want to pass a message to your Activity, but you don`t want to wake it up, you should implement your own Message Stack.
The thread should send messages to your stack and once your activity is resumed, it should read the stack, but this way you have no guarantee that the activity will recieve the messages since it has to be bringed back to front from the user.
Using Intent ensures you that your activity will get the message in real time and will react.
Upvotes: 1