4

I have a widget on my home screen with several ImageButtons which have default background images. Through the configuration activity, I can change the image on any of the ImageButtons. The problem is that when the screen is rotated, the image on the ImageButton disapears and it changes back to the default image.

I don't know why this happens or how to fix it

alejom99
  • 177
  • 3
  • 11

2 Answers2

10

When the screen is rotated the entire appwidget is rebuilt using the last RemoteViews object you passed to AppWidgetManager.updateAppWidget(). Thus is it very important that every time you call updateAppWidget() you pass a RemoteViews object that has everything set on it that the widget needs if it were to be completely rebuilt, not just one or two things that you want to update on the widget display.

So, in your AppWidgetProvider class, whenever you are updating your appwidget you need to create a RemoteViews object, build up all the view settings for your appwidget using that object, and then make one call to AppWidgetManager.updateAppWidget() when you are done.

My guess is you are doing something like this:

  • Get RemoteViews Object
  • Set the new button image
  • Call updateAppWidget()
  • Get RemoteViews Object
  • Set a pending intent on the button
  • Call updateAppWidget()

When you need to be doing something like this:

  • Get RemoteViews Object
  • Set the new button image
  • Set a pending intent on the button
  • Call updateAppWidget()
Mark B
  • 183,023
  • 24
  • 297
  • 295
  • What if you are updating from two locations. Aka `OnRecieve` and `OnUpdate` – IAmGroot Sep 17 '12 at 15:51
  • @Doomsknight why does that make a difference? Just update it correctly in both. I'm not sure how your question applies to the original question being asked here. – Mark B Sep 17 '12 at 17:00
  • Because I have two different RemoteView updates. One that updates half and another that updates the other. If it is refreshed on the last RemoteView, then It will only ever restore 50% of my data. – IAmGroot Sep 17 '12 at 17:19
  • @Doomsknight I think you need to ask that as a separate question, with some example code. I'm not following what you are saying. – Mark B Sep 17 '12 at 17:40
1

mbaird's answer is the one.
But there's another solution though. The solution is to use updateAppWidget when you're creating/updating the whole view of the widget and partiallyUpdateAppWidget when you're changing only part of the view. Then the view is not reset.

Partial update example (show a little sync icon on all widgets):

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);    
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, Widget.class)); 
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout); 

views.setViewVisibility(R.id.syncImage, View.VISIBLE);

for (int i = 0; i < appWidgetIds.length; i++) {    
    appWidgetManager.partiallyUpdateAppWidget(appWidgetIds[i], views);
}
dorsz
  • 905
  • 10
  • 20