As a result, an IntentReceiver leaked, which was originally registered here. Are you missing the unregisterReceiver () call?

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?

+54
android
Jan 16 '13 at
source share
6 answers

Do not rely onStop () because :

Please note that this method can never be called in low memory situations, when the system does not have enough memory to support your activity process after its onPause () method is called

Read more about the life cycle of an activity here .

Unregister your receiver in onPause ():

 @Override protected void onPause() { super.onPause(); unregisterReceiver(yourReceiver); } 
+47
Jan 07
source share
— -

You need to unregister the recipients when you stop your activity:

 @Override protected void onStop() { unregisterReceiver(yourReceiver); super.onStop(); } 
+42
Jan 16 '13 at 11:43
source share

You can unregister as soon as you receive a broadcastreceiver

  @Override public void onReceive(Context context, Intent intent) { getActivity().unregisterReceiver(this); } 
+4
Oct 28 '13 at 20:16
source share

Just add to the answers above, if you register the recipient in onCreate, it must be unregistered in onDestroy. if you register the recipient with onResume, you must unregister it with onPause.

Remember where you register and unregister the recipient, for example, if you register the recipient in onCreate (Bundle) using the activity context, you must unregister it in onDestroy () to prevent the recipient from leaking from the activity context. If you register the recipient with onResume (), you must unregister it with onPause () to prevent it from being registered multiple times (if you do not want to receive broadcast messages during a pause, and this can reduce unnecessary system overhead). Do not unregister onSaveInstanceState (Bundle) because it is not called if the user returns to the history stack.

Source

+4
Jan 22 '18 at 1:34
source share

Unregistering your receiver on ontop () is a valid answer. Do not call it the onPause () method.

 @Override protected void onStop() { unregisterReceiver(yourReceiverName); super.onStop(); } 
+2
Jul 28 '16 at 1:34
source share

Make sure the recipient is registered before unregistering it. do this, declare a boolean

 private boolean isReceiverRegistered = false; 

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

 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. } } 
0
Dec 19 '18 at 8:12
source share



All Articles