Activity has leaked IntentReceiver that was originally registered here. Are you missing a call to unregisterReceiver()?

Android

Android Problem Overview


01-16 16:52:42.211: E/ActivityThread(2529): Activity com.Civilcourage.CivilcurageSplash has leaked IntentReceiver com.google.android.gcm.GCMBroadcastReceiver@405282e0 that was originally registered here. 

Are you missing a call to unregisterReceiver()?

What is the cause of the above error? How can it be avoided?

Android Solutions


Solution 1 - Android

Don't rely on onStop(), because:

>Note that this method may never be called, in low memory situations where the system does not have enough memory to keep your activity's process running after its onPause() method is called

More on Activity life cycle here.

Unregister your receiver in onPause():

@Override
protected void onPause() {
    super.onPause();

    unregisterReceiver(yourReceiver);
}

Solution 2 - Android

You need to unregister your receivers on stop of your activity:

@Override
protected void onStop()
{
    unregisterReceiver(yourReceiver);
    super.onStop();
}

Solution 3 - Android

You can unregister as soon as you receive the broadcastreceiver

 @Override
 public void onReceive(Context context, Intent intent) {
     getActivity().unregisterReceiver(this);
 }

Solution 4 - Android

Just to add to the answers above, if you register a receiver in on onCreate, it should be unregistered in onDestroy. if you register a receiver on onResume, you should unregister it in onPause.

>Be mindful of where you register and unregister the receiver, for example, 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). Do not unregister in onSaveInstanceState(Bundle), because this isn't called if the user moves back in the history stack.

Source

Solution 5 - Android

Make sure receiver is registered before you unregister it. to do this, declare a boolean

private boolean isReceiverRegistered = false;

then right after calling registerReceiver() method, set the flag isReceiverRegistered to true as shown in the below code snippet

registerReceiver(broadcastReceiver, new IntentFilter("anyString"));
    isReceiverRegistered = true;

then, in onPause()

 @Override
protected void onPause() {
    super.onPause();
    if(isReceiverRegistered){
        unregisterReceiver(broadcastReceiver);
        isReceiverRegistered = false;// set it back to false.
    }
}

Solution 6 - Android

Unregister your receiver on onStop() is the valid answer. Do not call it on onPause() method.

@Override
protected void onStop()
{
    unregisterReceiver(yourReceiverName);
    super.onStop();
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRajView Question on Stackoverflow
Solution 1 - AndroidMelquiadesView Answer on Stackoverflow
Solution 2 - AndroidNermeenView Answer on Stackoverflow
Solution 3 - AndroidOmid AminivaView Answer on Stackoverflow
Solution 4 - Androidthe_proleView Answer on Stackoverflow
Solution 5 - AndroidSoropromoView Answer on Stackoverflow
Solution 6 - AndroidArchaView Answer on Stackoverflow