Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are you missing a call to unregisterReceiver() ? in android

I get the following error :

"Are you missing a call to unregister receiver"

android.app.IntentReceiverLeaked: 

Activity com.apps.activities.MainActivity has leaked IntentReceiver com.wwhere.fragment.MainRecyclerViewFragment$1@44d50ba8 that was originally registered here. Are you missing a call to unregisterReceiver()? at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:809)

at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:610)
at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1478)
at android.app.ContextImpl.registerReceiver(ContextImpl.java:1458)
at android.app.ContextImpl.registerReceiver(ContextImpl.java:1452)
at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:467)

My code is :

private BroadcastReceiver actionConversation = new BroadcastReceiver(){

    @Override
    public void onReceive(Context context, Intent intent) {
        setAdapter();
    }
};

private void registerBroadCasts() {
    IntentFilter intentConnection = new IntentFilter(Constants.CONVERSATION_SCREEN);
    getActivity().registerReceiver(actionConversation, intentConnection);
} 

And i call registerBroadCasts() in onCreate method.

like image 430
Kinjal Avatar asked May 16 '15 05:05

Kinjal


1 Answers

I guess you are using GCM in your application.

In the activity where you are registering a device to GCM,write the following code:

@Override
public void onDestroy() {

    try{
        if(mHandleMessageReceiver!=null)
            unregisterReceiver(mHandleMessageReceiver);

    }catch(Exception e){}

    super.onDestroy();
}

where mHandleMessageReceiver is the instance of the BroadcastReceiver.

like image 108
kgandroid Avatar answered Sep 19 '22 14:09

kgandroid