Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Widget: Show configuration activity before widget is added to the screen

I have an Android Widget that uses web services to retrieve and display the data on the widget. The widget has a configuration activity that extends PreferenceActivity. The configuration activity starts up as soon as the widget is installed, which is the desired behavior for this widget.

The problem is, whenever a widget is added to the home screen, the widget attempts to update iteself before the configuration activity is started/completed which may potentially lead to a long delay (several seconds). The configuration activity should occur before the widget attempts to update itself anytime a new widget is added.

Here is the sequence of events that I'm seeing in LogCat when a widget is added:

  1. Widget.onRecive: action = APPWIDGET_ENABLED
  2. Widget.onEnabled
  3. Widget.onReceive: action = APPWIDGET_UPDATE
  4. Widget.onUpdate: Widget Service is started.
  5. WidgetService.onStartCommand: Potentially long running work which will delay the configuration activity from being immediately shown.
  6. WidgetConfiguration.onCreate
  7. Widget.onReceive: action = APPWIDGET_UPDATE
  8. Widget.onUpdate: Widget Service is started again
  9. WidgetService.onStartCommand: Potentially long running work is performed again.

What's happening is that when a widget is added, the service will start up before the configuration view has been shown.

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="xxx.xxx.xxxwidget"
    android:versionCode="1"
    android:versionName="@string/app_version" >

    <uses-sdk android:minSdkVersion="8" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:debuggable="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <receiver android:name="xxxWidget" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/widget_info" />
        </receiver>

        <activity android:name="xxxWidgetConfigure" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name="xxxWidgetService" />
    </application>

</manifest>



Question
Is there a way to force the configuration activity to be shown before the system attempts to add the widget to the home screen?

like image 773
Metro Smurf Avatar asked Feb 04 '12 20:02

Metro Smurf


1 Answers

From android documentation: http://developer.android.com/guide/topics/appwidgets/index.html#Configuring

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.

HOWEVER, this does not seem to be correct!

What I did was adding a boolean to SharedPreferences which tells me if this widgetId has been through configuration. If not skip this update. Implement this in your AppWidgetProvider class' onUpdate method.

like image 92
Spiff Avatar answered Oct 24 '22 20:10

Spiff