Solve error has leaked IntentReceiver on Android


I have an extended class of BroadcastReceiver in I launch the listener

        new ConnectivityChangeReceiver(),
        new IntentFilter(

I find that when I leave the app it shows me the following error in the log:


E / ActivityThread: Activity has leaked IntentReceiver   [email protected]32e   that was originally registered here. Are you missing a call to   unregisterReceiver ()?                Activity has leaked IntentReceiver   [email protected]32e   that was originally registered here. Are you missing a call to   unregisterReceiver ()?


that was originally registered here. Are you missing a call to   unregisterReceiver ()

In onStop() I would like to deregister the listener, but not as implement it

asked by Webserveis 29.06.2016 в 11:38

2 answers


You should save your BroadcastReceiver in a variable of your MainActivity and in your onStop() put unregisterReceiver(myBroadcastReceiver);

You should keep in mind that if you go through the onStop() without having registered the BroadcastReceiver previously, an exception will be thrown.


answered by 29.06.2016 / 12:01

When implementing a BroadcastReceiver , it is important to know that we need to register and deregister the receiver , but It is NOT good practice to cancel the receiver on onStop () and this is the reason, which you can see in the documentation:

When your implementation requires registration in onResume() there is a consideration important .


If you are registering a receiver in Activity.onResume() , you must cancel   the record in Activity.onPause() . (You will not receive intents when you are in   pause, and this can reduce unnecessary system overhead)

Regarding your error:


MainActivity has leaked IntentReceiver

the documentation itself points to it:


Do not forget to cancel the registered receiver register in   dynamic using the Context.unregisterReceiver() method. If you forget   of this, the Android system reports a Leaked error. For example, yes   You registered a receiver in onResume() of your activity, you must    cancel registration in the onPause() method.



In the onResume () method, the Recording receiver.


In the onPause () method, the deregistration of the receiver.

I add an example:

public class MyActivity extends Activity

  private final BroadcastReceiver mybroadcast = new BroadcastReceiver ();

  public void onResume()
    IntentFilter filter = new IntentFilter();
    registerReceiver(mybroadcast, filter);  


  public void onPause()
answered by 29.06.2016 в 17:01