1

if you register a receiver in onCreate(Bundle) using the activity's context, you should unregister it in onDestroy() to prevent leaking the receiver out of the activity context. If you register a receiver in onResume(), you should unregister it in onPause() to prevent registering it multiple times (If you don't want to receive broadcasts when paused, and this can cut down on unnecessary system overhead).

I got this from the official documentation. Why we say "if we register a receiver in onCreate(), we should unregister it in onDestroy()"?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Henok Tesfaye
  • 8,287
  • 13
  • 47
  • 84
  • My question is not when to register and unregister a Broadcast Receiver. – Henok Tesfaye May 19 '18 at 15:55
  • that link or similar links has already has answers for this question . See `Activity` lifecycle. – ADM May 19 '18 at 16:27
  • 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. – billa-code May 19 '18 at 16:38

2 Answers2

2

You should unregister the receiver in the corresponding event of the one where you register it. This will prevent registering/unregistering in an unbalanced way.

For instance, let's say you registered it in onCreate, but you're unregistering in onPause. Your activity is created and registers the receiver. Then you show a dialog. onPause is called and you unregister the receiver. Now the dialog is dismissed. onResume is called and your activity continues. But it's not recreated, onCreate won't be called and your the receiver was already unregistered, you won't get broadcasts anymore in this activity.

That's why it's important to use the correct pair of lifecycle events.

And a little explanation of those events you mentioned:
onCreate happens when the Activity is created and onResume when it goes into foreground. The corresponding closing events are onPause when it leaves the foreground, e.g., a dialog covers the Activity partially or the user navigates away from the Activity, with another one covering it fully (in that case, onStop would also be called). If the Activity is destroyed due to a configuration change (e.g., rotation) or the system reclaiming it, onDestroy is called.

A little digression here: specifically about onCreate/onDestroy, I would avoid that pair for broadcast receiver registration. The reason is that onDestroy may take too long to be called or may never be called (as per Android specification, it's not guaranteed, the activity's memory may be collected without calling onDestroy). Another example: your activity is created, you register the receiver. Then you navigate from that activity to another. It receives onPause and then onStop, you're in another activity, but the previous one is still listening to broadcasts.

1

1 - if you register a receiver in onCreate(Bundle) using the activity's context, you should unregister it in onDestroy() to prevent leaking the receiver out of the activity context

onCreate() of an Activity is called when the Activity is created for the first time and onDestry() of an Activity is called when that Activity is destroyed. Both functions are called only once in lifetime of an Activity.

So, if you register a broadcastReceiver in onCreate() then you will have to unregister it in onDestroy() so that this registered broadcastReceiver does not affect to other Activity after the destruction of this Activity.

2 - If you register a receiver in onResume(), you should unregister it in onPause() to prevent registering it multiple times

onResume() is called whenever this Activity is active and visible and onPause() is called whenever this Activity is invisible or inactive.

So, if you register a broadcastReceiver in onResume() then this is called whenever Activity is visible and active, so it will be registered multiple times in one lifecycle of an Activity, so you will have to call unregisterReceiver in onPause(), this will unregister the registered receiver whenever you Activity is invisible and inactive.

Ghulam Moinul Quadir
  • 1,638
  • 1
  • 12
  • 17