When to register / unregister broadcast receivers created in action?

I need to create a custom broadcast receiver in an onCreate event of activity, and obviously I need to disable a broadcast receiver in an onDestroy event of activity

For clarity, this is a piece of code that I use

public class AnActivity extends Activity { private ResponseReceiver receiver; public class ResponseReceiver extends BroadcastReceiver { public static final String ACTION_RESP = "mypackagename.intent.action.MESSAGE_PROCESSED"; @Override public void onReceive(Context context, Intent intent) { // TODO Start a dialogue if message indicates successfully posted to server } } /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); IntentFilter filter = new IntentFilter(ResponseReceiver.ACTION_RESP); filter.addCategory(Intent.CATEGORY_DEFAULT); receiver = new ResponseReceiver(); registerReceiver(receiver, filter); } @Override public void onDestroy() { super.onDestroy(); unregisterReceiver(receiver); } 

I read that onPause / onResume and onStart / onStop events for activity should also register and unregister the broadcast receiver.

I really want to understand what is considered best practice for this and why.

+70
android broadcastreceiver
Oct 25 '11 at 9:21
source share
5 answers

You must register onStart() register your recipients onStart() and onStop() .

The only reason BroadcastReceiver registers activity is to somehow use the events in the current activity to inform the user about the event. If onStop() , then the Activity no longer in the foreground, and therefore cannot update the user.

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

As Konstantin says, onDestroy() not guaranteed to be called, and you can continue to receive transmissions for a long time when the Activity no longer open.

+82
Oct 25 2018-11-11T00:
source share

Since onDestroy() cannot be called, you must use onPause() to unregister. Consider the life cycle of your broadcast receiver: do you need it to be active only when your activity is in the foreground? Then use onResume() / onPause()

+19
Oct 25 2018-11-11T00:
source share

The Android documentation does not prescribe a separate place to register / unregister broadcast receivers, but mentions onStart() / onStop() and onResume() / onPause() as features.

The most important factor in making this decision is when should your receiver be able to do its job? This will determine when to register and unregister.

  • Does the receiver need to do something with the broadcast only when the focus is in focus? If so, you can register / onPause() registration with onPause() / onReceive() . (You can also use a longer lifespan, for example, onStart() / onStop() , but then you should check if the action is focused on the receiver onReceive() .)

  • Does the receiver need to do something when it is visible, even if it has no focus (for example, when a dialog box is displayed)? If so, use onStart() / onStop() (or a longer lifetime, but again, the onReceive() receiver should check to see if activity is visible).

  • Does the receiver need to know about the broadcast, even if the activity is not visible? For example, is it necessary to remember that something happened so that when the activity became visible, it could reflect the final state of affairs? Then you need to use onCreate() / onDestroy() to register / unregister. (Note that there are other ways to implement this kind of function.)

If you register with onStart() , do not register them with onResume() , because it will be redundant: onResume() never onStart() without onStart() first.

Also keep in mind that it is best to keep onPause () as easy as possible :

Running onPause () is very short and does not always give enough time to perform save operations. For this reason, you should not use onPause () to save application or user data, make network calls, or perform database transactions; such work may not be completed before the completion of the method. Instead, you should perform shutdown operations under heavy load during onStop ().

It is true that onDestroy() not guaranteed to be called if the system kills your process to save memory. However, if the process is killed, the process will still not receive broadcasts. In this case, is it really necessary to unregister broadcast receivers?

+8
Jun 13 '17 at 16:12
source share

Android can kill your application using the onStop() method. The best way to resolve this situation is to register the BroadcastReceiver method in onResume() and unregister it in onPause() .

+5
Oct 14 '16 at 14:18
source share

You must register and unregister your translation in the onResume () and onPause () methods.

if you register with onStart () and unregister with onStop (). This time you will get the following problem.

if your device’s screen is locked, onStop () is called, and if you unlock this time, onStart () is never called. This is why you have registered and unregistered in the onResume () and onPause () methods.

0
Apr 04 '19 at 8:51
source share



All Articles