When testing your Android App Widget, the layout views disappears or get deleted after changing screen orientation to landscape
In my case i was using "RemoteViewsService.RemoteViewsFactory" to populate a ListView inside the widget
Solution
Below paragraph copied from here http://stackoverflow.com/questions/2653270/android-widget-imagebutton-loses-image-when-screen-is-rotated
"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"
--------------------------------
So in my case i was using a code to update only one field in the widget and then i run appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
The system caches this last update which includes only one element populated so that when i change orientation it show that cached RemoteView
My Solution:
Instead of using "updateAppWidget" use "partiallyUpdateAppWidget" which does not cause RemoteViews caching
Example
RemoteViews widget=new RemoteViews(this.getPackageName(), R.layout.widget);
widget.setTextViewText(R.id.loading, updatedText);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.partiallyUpdateAppWidget(appWidgetId,widget);
partiallyUpdateAppWidget documentation
http://developer.android.com/reference/android/appwidget/AppWidgetManager.html#partiallyUpdateAppWidget%28int,%20android.widget.RemoteViews%29
Tweet
No comments:
Post a Comment