Avoid Registering Duplicate Broadcast Receivers In Android
I am trying to create my first Android app. I would like a main-thread Activity (in my case: an ActionBarActivity) to receive notification of an event from a background Activity (i
Solution 1:
To answer your question, you can ignore sharedprefs and register the broadcast receiver in onResume
and unregister the receiver in onPause
. This method is well described in Reporting Work Status(1) android doc. Note that, this way, you will get status updates only if Activity is in foreground. Use appropriate life cycle methods depending on your needs.
To report status back to foreground activity from service, my preference would have been ResultReceiver
(2) class which feels natural compared to a broadcast. Also, if you need to report multiple status messages back, it will be clearer with statusCode
param in onReceiveResult
method of ResultReceiver class.
Post a Comment for "Avoid Registering Duplicate Broadcast Receivers In Android"