When to Register/Unregister Broadcast Receivers Created in an Activity

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.

registering and unregistering broadcast receivers?

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.

Is unregisterReceiver absolutely required for a context-based Android Broadcast Receiver?

I believe your understanding is correct - once the activity is destroyed, you should no longer receive broadcasts. However, since you haven't unregistered, you may still leak the object. (At the very least, you'll get unhappy warning messages in your logcat about this)

Just from a code cleanliness standpoint, it feels like you should have a cleanup method as well. In the future, you may extend what the "DoesManyThings" object does, and other state you add may be less safe to hold on to outside the lifecycle of an Activity.

However, note that onDestroy is not guaranteed to be called. You may wish to consider moving to onStart/onStop - but that depends on your exact use case for the dynamic receiver.

Do broadcast receivers registered with an activity's context still respond if the activity goes into onStop?

As stated in https://developer.android.com/guide/components/broadcasts#context-registered-receivers :

Context-registered receivers receive broadcasts as long as their registering context is valid. For an example, if you register within an Activity context, you receive broadcasts as long as the activity is not destroyed. If you register with the Application context, you receive broadcasts as long as the app is running.

So I think it's the onDestroy method of the activity lifecycle that should be your concern.

Registering and unregistering BroadcastReceiver in a fragment

Have a look at life-cycle of Fragments:

onCreateView():
The system calls this when it's time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View from this method that is the root of your fragment's layout. You can return null if the fragment does not provide a UI.

onResume():
The fragment is visible in the running activity

onPause():
This is usually where you should commit any changes that should be persisted beyond the current user session (because the user might not come back).

Conclusion:

So it is better to register the receiver only inside onResume() and unregister inside onPause() because onCreateView() deals with view hierarchy only. It has nothing to do with receiver. So it is not harmful but surely it is useless.

I hope it will be helpful!!

is the broadcast receiver registered via manifest is automatically unregister on app close?

Android BroadcastReceiver declared in the manifest are not automatically unregistered. Those can be triggered even when the app is not running (that is the expected behavior too). When a BroadcastReceiver is triggered, the onReceive callback method is called. The broadcast is only active during the execution of the onReceive method. Once the method is returned, the receiver is considered as inactive. So, if you want to declare a BroadcastReceiver which can be triggered always (even when the app is not running), you declare it in the manifest. And you don't have to worry about its lifecycle.

However, if you want to declare a broadcast which will receive broadcast only during a certain amount of time (e.g., when app is running or when an activity is running), then it is better to register the broadcast when needed and unregister it when the work is done. For example, if you want to receive a broadcast only when an activity is running, then you can register in in the onCreate method and unregister it in the onDestroy method.

How to unregister broadcast receiver from an Application Class?

One suggestion is in your launcher activity or desired activity unregister that like this:

@Override
protected void onDestroy() {
super.onDestroy();
((MyApplication) getApplication()).unregisterReceiver();
}

And implement unregisterReceiver method in MyApplication class:

public void unregisterReceiver() {
unregisterReceiver(br);//your broadcast
}


Related Topics



Leave a reply



Submit