Auto Screen Rotation Disabling Widget RemoteView o

2019-02-17 01:40发布

I have a bit of a strange bug with a widget I've coded- after the screen rotates the widget stops responding to onClick events. The code is exactly the same as in the Android Developer Documentation for App Widgets here. I've noticed other widgets from the market don't have this problem- is there a known workaround perhaps? I've tried tapping all over the place after a rotation so I don't think its the onClickPendingIntent not being resized after a rotation; it doesn't seem to be present at all.

I can't find an onRotation() kind of trigger for the AppWidgetProvider to redo the listening code in the event of a rotation so I'm quite unsure how to proceed...

Thanks!

3条回答
聊天终结者
2楼-- · 2019-02-17 02:12

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

查看更多
我只想做你的唯一
3楼-- · 2019-02-17 02:17

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:

final RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.app_widget_4x2);
remoteViews.showNext(R.id.appWidget_viewFlipper);
appWidgetManager.partiallyUpdateAppWidget(widgetId, remoteViews);
查看更多
小情绪 Triste *
4楼-- · 2019-02-17 02:18

The following code seems like being able to resolve 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});
}
查看更多
登录 后发表回答