Search code examples
androidandroid-intentandroid-activitybroadcastreceiver

When does an Activity receive broadcast in Android


Concerning Broadcast Receiver running inside an Activity. Normally I register inside onResume and unregister inside onPause. But. Say my Activity was paused when a sender sent a broadcast and now my activity is resumed. Will the Activity, now resumed, receive the broadcast?

Now understand this. The Docs say the Activity will not receive intent when paused. But the statement is dangling for my particular inquiry.

onCreate -> onResume -> onPause|...broadcast sent...| -> onResume-> ???

Solution

  • No. Because by the time you're sending the broadcast to that activity, you have already unregistered that receiver. Therefore, the broadcast will be sent, look for a receiver that can handle that broadcast, and since there is none, drop that request.

    On resume, your app will re-register the receiver. However, since the previous request was dropped, it will not respond to it, but only to requests sent when the activity has resumed.