10

I have a simple appwidget and I want to update it when an action occurs in an activity (in the same app). in onUpdate(), I immediately update the widget, which works fine. In my activity, I call the same static update method in my appwidget that is called in onUpdate() to update the views. the widget is not updated.

I can trace the code right into the AppWidgetManager.updateAppWidget() method, and all this good, but the widget does not update.

The only possible difference I can see is that the context object passed into my static update method is different, when it's called from the context of an activity vs. the context of a appwidget's onUpdate() method. however, there are lots of examples on the web of this so I expect it should work.

Darren
  • 68,902
  • 24
  • 138
  • 144
Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134

2 Answers2

23

Without seeing your code I'm not 100% sure how you are trying to do it, however here is the method I use. Within my Activity I have the following method:

private void updateAllWidgets(){
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
    int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(this, MyWidget.class));
    if (appWidgetIds.length > 0) {
        new MyWidget().onUpdate(this, appWidgetManager, appWidgetIds);
    }
}

Where MyWidget is the class of the appwidget. I can call this method from anywhere within my Activity to update all my appwidgets.

dave.c
  • 10,910
  • 5
  • 39
  • 62
  • 1
    that made things work, so the problem must be that i was not updating the correct id. as it was, the widget sets up an onclick intent with its id, which is received by the activity, which then tells the widget to update itself. – Jeffrey Blattman Dec 14 '10 at 19:13
  • I don't think calling the onUpdate method is the correct approach. You should send a broadcast for AppWidgetManager.ACTION_APPWIDGET_UPDATE. – midhunhk Jun 30 '14 at 09:36
  • @silverback from a "doing it correctly" approach, I agree with you, that the right way to do it is to send a broadcast. From a "just make it work" approach, the above will cause the app widgets to update - dirty but functional. – dave.c Jun 30 '14 at 10:26
  • @dave.c, yes you are right. This method should work, but since this is part of the API, I was saying that we should ideally allow the platform to call the onUpdate() method. There could be no guarantee that the platform might do some other operations before calling the onUpdate() method. Some other issue can prop up in such case. – midhunhk Jul 02 '14 at 09:31
10

Rather than use a static method take advantage of the fact that the widget is already a broadcast receiver and register an update intent for it in your manifest. Then whenever you need to update it from the activity just call

//in your activity
sendBroadcast(new Intent(MyWidget.ACTION_UPDATE).putExtra(whatever));  



//In widget class
public static final String ACTION_UPDATE = "com.example.UPDATE_MY_WIDGET";

and inside the receiver tags of your manifest file

<intent-filter>
  <action android:name="com.example.UPDATE_MY_WIDGET"/>
</intent-filter>
Nathan Schwermann
  • 31,285
  • 16
  • 80
  • 91
  • i was doing this, but i changed it so the widget just starts an intent service. why? the widget performs a potentially long-ish running operation. i was getting ANR in the widget when it onUpdate() didn't return promptly. not sure if this is related to my problem. – Jeffrey Blattman Dec 14 '10 at 19:11
  • yes any long operation should use an IntentService, AsyncTask or some other type of Threading. My answer here is how I would go about informing your widget that its time to start the task -or- to inform your widget that the task is complete and needs to update its state. – Nathan Schwermann Dec 14 '10 at 19:19
  • @schwiz Long time ago but still, you should start a service in your onUpdate() to perform the long operation. The receiver I think gets about 5 seconds to execute. Creating an AsyncTask is inadaquate because it too would be killed when the receiver gets destroyed. – HGPB Feb 19 '13 at 13:45