onUpdate not being called in widget, even though I see the android.appwidget.action.APPWIDGET_UPDATE intent in onreceive
Asked Answered
G

4

12

I'm working with the emulator using the debugger and I've noticed that onUpdate() is not getting called. When I add the widget to the emulator homescreen I see my breakpoint being hit in the onReceive method. The onReceive() method does get the android.appwidget.action.APPWIDGET_UPDATE intent. However, the onUpdate() method never gets called. I believe it's defined correctly.

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds)  {
    // code that sets up remove view and updates appWidgetManager
}
Gazette answered 14/7, 2010 at 4:49 Comment(0)
T
25

Call the super class method in onReceive()

@Override
public void onReceive(Context context, Intent intent)
{
    // Chain up to the super class so the onEnabled, etc callbacks get dispatched
    super.onReceive(context, intent);
    // Handle a different Intent
   Log.d(TAG, "onReceive()" + intent.getAction());

}

If you override onReceive() in your subclassed AppWidgetProvider, then the provider's onEnabled, onUpdate, etc callbacks are not triggered unless you call the superclass.

Tingley answered 2/8, 2012 at 19:28 Comment(0)
H
1

onUpdate is called by the onReceive only if you set the AppWidgetManager.EXTRA_APPWIDGET_IDS extra to an array of appWidgetIds i.e

    // Set the update intent for when the sync button is clicked.
    Intent syncIntent = new Intent(context,Widget.class);
    syncIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
    syncIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds); // onUpdate is only called when AppWidgetManager.EXTRA_APPWIDGET_IDS is set to a non empty array.
    PendingIntent syncPendingIntent = PendingIntent.getBroadcast(context,1, syncIntent,0);
    rv.setOnClickPendingIntent(R.id.updateWidgetImageButton, syncPendingIntent);
Hufuf answered 19/11, 2021 at 4:22 Comment(1)
Also, the appWidgetIds array cannot be empty. But it can contain random placeholder values if you're just going to ignore it anyway.Patellate
H
0

Do you have a configuration activity setup for your widget? Because if you do, then onUpdate is not called and it is the job of the configuration activity to manually update the widget for the first time. After that onUpdate should be called as defined in your updatePeriod configuration.

Hemihydrate answered 14/7, 2010 at 8:50 Comment(1)
Yes I read that as well, but in thi scase I'm not using a configuration activity and don't have one specifiedGazette
T
0

Do you have this in the Android Manifest?

       <intent-filter>
   <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
  </intent-filter>
Tasia answered 13/10, 2010 at 22:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.