Let's say I have an Activity
. In its onCreate
lifecycle callback I create a BroadcastReceiver
object by implementing it as an anonymous class and put some code inside its onReceive
callback that requires Activity
's Context
to show a Toast
message when it gets called. In Activity
's onResume
callback I register that BroadcastReceiver
and in onPause
I unregister it, because it contains code that requires Activity
's Context
, and it might not be available if Activity
is going through the destruction procedure, i.e. orientation change. This means that if something, i.e. IntentService
broadcasts an Intent
for that BroadcastReceiver
while it's unregistered (no matter how short, but it still can happen), the Intent
will never get delivered and proper actions that handle it will never happen. So the user might miss some important information (message informing about a succesfull registration, warning that something went wrong etc.) Am I right? Is it true that such scenario may happen? How to prevent it? How to make sure that the BroadcastReceiver
will receive the broadcast no matter when some part of the application fires it?
Asked
Active
Viewed 486 times
1

Salivan
- 1,876
- 7
- 26
- 45
-
1do not use `BroadcastReceiver` in such case, use a "bound service" pattern instead – pskink Nov 15 '15 at 21:22
-
I've totally forgotten about a thing like that. Haven't been developing for Android for quite a while :(. I'll think of a solution using the bound service, but I'd still appreciate getting some simpler solution advice. – Salivan Nov 15 '15 at 21:51
1 Answers
1
Create MyBroadcastReceiver
extending BroadcastReceiver
(of course :D) and register that in the manifest with the appropriate intent filter action. Then in onReceive()
you'll have access to Application's Context
. Keep in mind that onReceive()
will be triggered even if your app is closed (if your action is Intent.ACTION_SCREEN_OFF
for example).
It depends on what you're trying to do in that receiver...also have a look at EventBus https://github.com/greenrobot/EventBus if you have to send events in your app.

Andrei Verdes
- 993
- 8
- 23
-
In that `BroadcastReceiver`'s `onReceive` I just want to show a message for a user, but that message may be shown in a form of a `TextView`, but without `Activity`'s `Context` reaching that `TextView` is meaningless :). – Salivan Nov 15 '15 at 21:48
-
Ok, who sends the message? If I get this right, you want to push the messages inside a queue when they arrive and when the `Activity` opens, show those messages? You may want to consider saving those messages...I don't think I understand why would you want to show the messages when the activity isn't open. – Andrei Verdes Nov 15 '15 at 21:52
-
You understood me wrong. I just want to let the `Activity` know when i.e. some `Service` finishes its work. If the `Activity` is destroyed because user wished so, then the message shouldn't be displayed, but I'm afraid this can happen on an orientation change for example. That way the `Activity` may not receive the message making the user to wait for a message that have already been sent and will never be again. – Salivan Nov 15 '15 at 21:55
-
Oh ok! In this case you may want to override `onConfigurationChanged()` this allows you to set the landscape layout without restarting the `Activity`. Have a look at: https://stackoverflow.com/questions/5961054/onstop-method-is-called-when-orientation-changes – Andrei Verdes Nov 15 '15 at 22:01
-
I'm looking for a simpler solution man :D. Reinventing all the orientation change logic just to make sure a message from `Service` is shown is not worth it :D. I think I'll consider risking that message will not get shown or implementing the bound service pattern if I decide that message is of such a great importance. But thanks for wonderful advices! – Salivan Nov 15 '15 at 22:04
-
By the way, I liked the idea about saving messages somewhere and displaying them when `Activity` starts. Will definitely use it somewhere in near future! – Salivan Nov 15 '15 at 22:11
-
1@Salivan `"... I liked the idea about saving messages somewhere and displaying them when Activity starts."` you save it in a Service and ask for a data when Activity binds to a Service, read about `"local bound service"` pattern [here](http://developer.android.com/guide/components/bound-services.html#Binder) – pskink Nov 16 '15 at 06:51