3

Somewhere here on Stack Overflow, I had found the following code some day which I adjusted to my application a bit:

private void updateWidget() {
    AppWidgetManager widgetManager = AppWidgetManager.getInstance(ctx);
    ComponentName widgetComponent = new ComponentName(ctx, MyAppWidgetProvider.class);
    int[] widgetIds = widgetManager.getAppWidgetIds(widgetComponent);
    Intent update = new Intent();
    update.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, widgetIds);
    update.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
    ctx.sendBroadcast(update);
}

This should programmatically refresh all instances of the application's widget. ctx is the Activity's context (this) that I set once in onCreate(). The method above is called in the Activity's onStop() method.

Unfortunately, when it is called, it replaces the app's widget by other apps' widgets (e.g. AP News) - at least for a while.

How can this happen? Is there something wrong in the code?

Thank you!

Edit #1: To point this out more clearly: I've already defined an interval for automatic refreshing. But in addition to that, I would like to update the widget from the Activity from time to time. This question suggests that it is possible as well.

Edit #2: I've just seen that the wrong widget is only shown for some seconds. After that, my own app's widget is shown again.

Community
  • 1
  • 1
caw
  • 30,999
  • 61
  • 181
  • 291
  • I had some problems with widgets in the past. All I did was moved the update code to a service. When I need to update, start that service. I'm not sure about that but the problem was solved. Could you try? –  Jun 30 '12 at 12:35
  • Thank you very much, this solved my problem! Don't know why (do you?), but it worked. So the `AppWidgetManager.getAppWidgetIds()` is not the problem, it works fine, as these ids are then passed to my service. Can you write it as an answer so that I can accept it? :) – caw Jul 02 '12 at 12:32
  • I'm glad it could help you. But I really don't know the reason, so I can't post it as an answer. You're welcome :-) –  Jul 02 '12 at 12:35
  • Oh, of course you can. Although it's not complete, it's the best answer here. So PLEASE post it! ;) An answer doesn't always require an explanation, most important is that it works. – caw Jul 02 '12 at 12:42

3 Answers3

4

I had some problems with widgets in the past, and have solved them. I really do not know the reason why my solution worked for me. And as it worked for Marco W., so I place it here: all I did was moved the update code to a service. When I need to update, start that service. Again, I'm not sure about that but the problems were solved.

Thanks Marco, I got some more experiences with Android. It's funny :-)

Community
  • 1
  • 1
  • 1
    Thank you, I'm glad it worked! :) Maybe someone else knows the explanation and will post it here some day ... – caw Jul 02 '12 at 13:00
  • Yes, it works! Strange but true. The only notice: should be used plain Service not IntentService – Barmaley Mar 03 '15 at 09:33
0

I solved this problem by using notifyAppWidgetViewDataChanged method which allows to update all my widgets, the explanation I can give is that the method works so as to update the collection view (StackView in my case) after modifying the data that it contains.

VMAtm
  • 27,943
  • 17
  • 79
  • 125
-1

Is there a reason you are running an update function outside of the widget? It seems to me that it would make more sense to uses the updatePeriodMillis property? You can set that property to how often you want the widget to call onUpdate() which can then provide the updated information.

http://developer.android.com/guide/topics/appwidgets/index.html#MetaData

Avery
  • 2,270
  • 4
  • 33
  • 35
  • This absolutely clear to me. Already doing that. Nevertheless, there are cases in which the thing above might be very useful. – caw Jun 24 '12 at 20:01
  • @MarcoW Take a look at this function [here](http://developer.android.com/reference/android/appwidget/AppWidgetManager.html#updateAppWidget%28android.content.ComponentName,%20android.widget.RemoteViews%29) Due to how it says it caches views, might it have another widget's views cached? – Avery Jun 27 '12 at 15:11
  • I don't know. How can I find this out? And what does this here mean? `This method will only work when called from the uid that owns the AppWidget provider` Does it work for my case? What is the `uid`? – caw Jun 27 '12 at 15:22