Search code examples
androidbroadcastreceiver

How to understand this sentences in developer.android.com about broadcastreceiver?


You won't receive intents when paused, and this will cut down on unnecessary system overhead

Full paragraphs: If registering a receiver in your Activity.onResume() implementation, you should unregister it in Activity.onPause(). (You won't receive intents when paused, and this will cut down on unnecessary system overhead). Do not unregister in Activity.onSaveInstanceState(), because this won't be called if the user moves back in the history stack.

Thank you!


Solution

  • This note from the documentation indicates some additional information regarding a special case for when a BroadcastReceiver is registered using an activity context. This case does not apply when a BroadcastReceiver is defined as a part of the AndroidManifest.xml file.

    Basically, the note indicates that a BroadcastReceiver that is registered to an Activity Context will not receive any broadcasted intents when that Activity is paused. Therefore, the BroadcastReceiver object which is registered to the Activity Context should be unregistered in the Activity.onPause() method. Unregistering the BroadcastReceiver in the Activity.onPause() method will remove it from memory so that no system resources are devoted to the meaningless registration.

    To be clear, the best use of system resources would be to register BroadcastReceivers in the Activity.onResume() method and unregister BroadcastReceivers in the Activity.onPause() method.