user1680782
user1680782

Reputation: 111

Widget. Activity has leaked IntentReceiver

In my Widget I use:

Intent intent = new Intent(context, ClassForWidget.class); 
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); 
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget1);            
views.setOnClickPendingIntent(R.id.layout_widget1, pendingIntent);`

And my class ClassForWidget already has a BroadcastReceiver:

BroadcastReceiver myReceiver = new MyReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(MyService.MY_ACTION);
registerReceiver(myReceiver, intentFilter);

so, when I click on Widget, I have error:

Activity .ClassForWidget has leaked IntentReceiver .ClassForWidget$MyReceiver@40ebe710 that was originally registered here. Are you missing a call to unregisterReceiver()?

How can I fix it? Thanks.

Upvotes: 1

Views: 1410

Answers (3)

user1730706
user1730706

Reputation: 348

Generally you should register in onResume and unregister in onPause (or onStart and onStop).

protected void onPause() {
     if (myReceiver != null){
         unregisterReceiver(myReceiver);
         myReceiver = null;
     }

}

Hope this helps you.

Upvotes: 2

Naresh J
Naresh J

Reputation: 2137

You need to unregister the braodcast receiver.

Add this line in your OnDestroy() method : unregisterReceiver(myReceiver);

Upvotes: 0

waqaslam
waqaslam

Reputation: 68187

I believe, you need to call unregisterReceiver() method in order to un-register the broadcast receiver. I'm not sure how you do it for widgets, but in case its an Activity, so we usually need to call this inside onStop().

Upvotes: 0

Related Questions