This is how I configure my APP's widget:
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="240dp"
android:minHeight="193dp"
android:updatePeriodMillis="86400000"
android:initialLayout="@layout/xyz_appwidget"
android:configure="com.xyz.activity.Configuration"
/>
Therefore, the system will bring up the Configuration
activity, once the widget has been put on the homescreen by the user. Unfortunately, without the configuration applied, this widget should not be put on the screen.
I added some debugging output. This happens when the user is selecting the widget for his homescreen. Note: This is when the Configuration activity is in front, NOT the widget.
I/ActivityManager( 101): Start proc com.xyz for broadcast com.xyz/.widget.xyz.XyzWidgetProvider: pid=14371 uid=10050 gids={3003, 1015}
D/XyzWidgetProvider(14371): onReceive: android.appwidget.action.APPWIDGET_ENABLED
D/XyzWidgetProvider(14371): onEnabled
D/XyzWidgetProvider(14371): onReceive: android.appwidget.action.APPWIDGET_UPDATE
So that means even the widget is not configured, both events (ENABLED, UPDATE) will be fired. Enabled makes sense to me, but UPDATE clearly not. Especially when the Configuration
activity finished successfully, no additional UPDATE event is being sent.
I also read that by setting the result of the Configuration
activity to Activity.RESULT_CANCELED
, one is able to cancel the configuration process. But as this clearly runs asynchronous I don't know how to make the configuration process blocking? Anyone encountered this before?
My final note: I had a look in the Android Gallery Widget, which somehow manages this properly, so a non-configured gallery widget will not be added. But I suspect some HTC magic there as the logs are not very helpful.
Here is how you handle this: In your configuration activity, do this in your onCreate(): setResult(Activity.RESULT_CANCELED);
Once the configuration activity is done, e.g. all parameters are set and widget should be shown, do setResult(Activity.RESULT_OK);
So, if the user does not finish the configuration activity, your widget is removed.
However, in the widget code itself (onUpdate), you still need to check whether configuration is complete, because APPWIDGET_UPDATE will definitely be called before configuration completion. You can just skip all updating altogether if configuration is not there, your widget will not be visible at this point anyway, and once configuration is complete, your widget is either removed by the system or its configuration is there.
One more thing. You need to initiate widget update from your configuration activity upon successful completion, because the system will not do it by itself.
So it seems the Widget api documentation on developer.android.com is obsolete...
https://groups.google.com/d/topic/android-developers/HfD-ojjsuso/discussion
"However, in the widget code itself (onUpdate), you still need to check whether configuration is complete, because APPWIDGET_UPDATE will definitely be called before configuration completion."
This is not the way it should be. On the android developer site you can read the following...
"The onUpdate() method will not be called when the App Widget is created (the system will not send the ACTION_APPWIDGET_UPDATE broadcast when a configuration Activity is launched). It is the responsibility of the configuration Activity to request an update from the AppWidgetManager when the App Widget is first created. However, onUpdate() will be called for subsequent updates—it is only skipped the first time."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With