Auto-rotate screen Disable widget RemoteView onClick event

I have a slightly strange error with the widgets that I encoded - after the screen rotates the widget, it stops responding to onClick events. The code is exactly the same as in the Android Developer Docs for application widgets here . I noticed that other widgets from the market do not have this problem - is there a known workaround? I tried to knock all over the place after rotation, so I don’t think that its onClickPendingIntent will not be changed after rotation; he does not seem to be present at all.

I cannot find the onRotation () trigger for the AppWidgetProvider to repeat the listen code in case of rotation, so I'm completely unsure how to proceed ...

Thanks!

+7
android android-appwidget
source share
3 answers

I received the following response from a post I made on Google Groups that solved my problem. I can’t say if it will solve the original problem with the poster, but I would publish it if someone else comes across this problem. Link to a post on Google Groups:

http://groups.google.com/group/android-developers/browse_thread/thread/ca8c2958b6dc086c#


When changing the configuration, there is no onUpdate. The home screen recreates your widget, then takes the most recent RemoteViews and applies it to the widget.

I thought it was a recreation of the widget while rotating. The problem is I do not seem to receive any messages about this, and in no way (I see) restore the connection. How can I detect that a rotation has occurred and set up a new onClick link?

As I said, you cannot (identify or respond to a change in orientation).

What you do, make sure that every time your code pushes the RemoteViews object into the home application for your widget, it is complete in every way:

  • Image resource identifier;
  • Has text bites;
  • Has pending intentions.

Do not do "incremental" updates to the widget, as if you were doing regular activity - first do not set intentions, then images, then text reflecting current information.

The home application starts as a separate process, and its state may fail with your widget receiver. When this happens, the only thing it has to recreate the widget is your last RemoteViews object. If it is complete, and has all parts, everything will work fine. If it only has the most recent text or image change, earlier updates that had intentions will be lost.

http://kmansoft.wordpress.com/2010/05/23/widgets-and-orientation-chan ...

- Kostya

+3
source share

The following code seems to solve the problem.

@Override public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) { super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions); onUpdate(context, appWidgetManager, new int[] {appWidgetId}); } 
0
source share

First, make sure your RemoteViews are a FULL view of the state of the widget if you call AppWidgetManager.updateAppWidget() . Configure all pending intentions, view data, etc. This state will be reused when the launcher wants to restore the widget from a state, for example. when the rotation changes.

If you want to update your remote views, but don't want to provide the full view of RemoteViews, i.e. you just want to change the existing remote view state, you can use AppWidgetManager.partiallyUpdateAppWidget() .

This update differs from updateAppWidget (int, RemoteViews) in that the RemoteViews object that is passed is understood as an incomplete view of the widget and therefore is not cached by the AppWidgetService. Please note that since these updates are not cached, state that they change that is not restored restoreInstanceState will not be saved if 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 promoting a ViewPager for a widget outside onUpdate:

 final RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.app_widget_4x2); remoteViews.showNext(R.id.appWidget_viewFlipper); appWidgetManager.partiallyUpdateAppWidget(widgetId, remoteViews); 
0
source share

All Articles