When to Register/Unregister Broadcast Receivers created in an activity?

You should register and unregister your receivers onStart() and onStop().

The only reason an Activity would register BroadcastReceivers is to use the events in some way on the current activity, to inform the User of an event. If onStop() has been called, then the Activity is no longer in the foreground, and therefore cant update the User.

If you want to receive broadcast events in the background you should consider using a service as indicated here.

Like Konstantin says, onDestroy() is not guaranteed to be called, and you could continue receiving broadcasts for a long time, when the Activity is no longer open.

Leave a Comment