MrJre
MrJre

Reputation: 7161

Appwidget IDs but no widgets

Im currently working on creating some appwidgets for my application; one of those is a widget containing a clock which needs an update every 60 seconds. I therefore create an alarm in the onEnabled() function of the AppWidgetProvider which sends a clock update intent every 60 seconds, which is received by the appwidgetprovider; this works like a charm, however id like to get rid of the timer when there are no more clock widgets as i like to keep things tidy. I therefore remove the alarm in the onDisabled() function of the AppWidgetProvider, as that one is called when the last of the widget type is removed.

The problem however is that even when I remove the last of the widgets, there are still widgetIds remaining in the appwidgetmanager and thus onDisabled() is never called. I tried rebooting the device, but they seem to persist.

Is this common behaviour and should i not rely on onDisabled() being called, is this a bug, or am i doing something terribly wrong?

Upvotes: 3

Views: 814

Answers (1)

Mark
Mark

Reputation: 558

Up to and including Android 1.6 widgets which fail to get added to the home screen are still in the list. I call them "phantom widgets". They get created when you try to add a widget but there's not enough space and you will get a message about that. But the widget somehow still makes it to the internal list. I think this was supposed to be fixed with 2.0 but I'm not sure.

In addition to that, using only onEnabled and onDisabled is a very bad idea for your purpose. Even when the screen is switched off your widgets will continue to get updated. Your device will never really sleep and the battery will drain too quickly. At least add a Receiver for ScreenOn and ScreenOff!

Upvotes: 1

Related Questions