Auto Screen Rotation Disabling Widget Remoteview Onclick Event
Solution 1:
I received the following response from a post I made on Google Groups which resolved my issue. I cannot say whether it would resolve the original poster's problem, but I though I would post it, in case someone else runs across this problem. The link to my Google Groups post is:
http://groups.google.com/group/android-developers/browse_thread/thread/ca8c2958b6dc086c#
There is no onUpdate on configuration changes. The home screen recreates your widget, then takes the most recent RemoteViews and applies it to the widget.
I figured that it was recreating the Widget on rotation. The problem is, I don't seem to be getting any messages to that effect, and have no way (that I can see) of re-establishing the connection. How can I determine that a rotate has happened and set up a new onClick connection?
Like I said, you don't (determine or respond to an orientation change).
What you do, is make sure that every time your code pushes a RemoteViews object into the home application for your widget, it's complete in all respects:
- Has image resource ids;
- Has text stings;
- Has pending intents.
Don't do "incremental" widget updates, like you would do with a regular activity - don't set the intents first, then the images, then the text reflecting current information.
The home app runs as a separate process, and its state can get out-of-step with your widget receiver. When it does, the only thing it has for re-creating your widget is your most recent RemoteViews object. If it's complete, and has all the parts, everything will work just fine. If it only has the most recent text or image change, the earlier updates which had the intents will be lost.
http://kmansoft.wordpress.com/2010/05/23/widgets-and-orientation-chan...
-- Kostya
Solution 2:
Firstly, ensure that your RemoteViews is a FULL representation of the state of the widget if you're calling AppWidgetManager.updateAppWidget()
. Set up all pending intents, view data, etc. This state will be re-used when the launcher wants to restore your widget from state, eg. when rotation changes.
When you want to update your remote views but you don't want to supply a complete RemoteViews representation, ie. you just want to change an existing remoteView state, you may use AppWidgetManager.partiallyUpdateAppWidget()
.
This update differs from updateAppWidget(int, RemoteViews) in that the RemoteViews object which is passed is understood to be an incomplete representation of the widget, and hence is not cached by the AppWidgetService. Note that because these updates are not cached, any state that they modify that is not restored by restoreInstanceState will not persist in the case that the widgets are restored using the cached version in AppWidgetService. Use with RemoteViews.showNext(int), RemoteViews.showPrevious(int), RemoteViews.setScrollPosition(int, int) and similar commands.
For example, when advancing a ViewPager for a widget outside of onUpdate:
finalRemoteViewsremoteViews=newRemoteViews(context.getPackageName(), R.layout.app_widget_4x2);
remoteViews.showNext(R.id.appWidget_viewFlipper);
appWidgetManager.partiallyUpdateAppWidget(widgetId, remoteViews);
Solution 3:
The following code seems like being able to resolve the problem.
@OverridepublicvoidonAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) {
super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
onUpdate(context, appWidgetManager, newint[] {appWidgetId});
}
Post a Comment for "Auto Screen Rotation Disabling Widget Remoteview Onclick Event"