12

Possible Duplicate:
After orientation change buttons on a widget are not responding

I'm facing a problem with an appwidget that has one ImageView inside the xml layout for which I register a pendingintent that is handled in OnReceive method . Everything works okay until I change phone orientation. At this point the widget doesn't work anymore, I click on the image but nothings happens. This problem is exactly as the one here : After orientation change buttons on a widget are not responding

What's is the problem and how can be resolved ? Thanks.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Alex
  • 2,213
  • 4
  • 32
  • 44
  • Hi, I answered the problem you have identified in the link you posted. When you rotate the screen it causes the widget to be recreated. When the widget is recreated, you need to rebind the on click handlers -- ie. attach the pending click events again. That's why the service works well for this scenario. It captures the onConfigurationChanged event and rebinds the handlers to the ui components. – jagsaund Jan 01 '11 at 19:13
  • 1
    Thank you, solved without the help of a service, just setting again the setOnClickPendingIntent in the OnReceive method. – Alex Jan 01 '11 at 19:52
  • 1
    can you tell us how you got onReceive() to be called for an orientation change? I'm having the same problem, but my AppWidgetProvider.onReceive() isn't getting called for this event. – Martin Stone Feb 23 '11 at 14:43
  • never mind: Solved it (documented below). – Martin Stone Mar 02 '11 at 12:22

4 Answers4

9

I eventually managed to recreate the OP's service-free solution. Here's the secret for the record: Any time you update the remote views, you must update everything that you ever update. My app was updating some visual elements, but not setting the button handler again. This caused the handler to stop working--not straight away--only after a rotation change, hence the confusion.

If this is done right, you don't need to intercept configuration change broadcasts, the last remote views you set will be used again after rotation. No call is needed to your AppWidgetProvider.

Martin Stone
  • 12,682
  • 2
  • 39
  • 53
2

I had a similar issue, but my app is working well now with the solution suggested by Alex

"...solved without the help of a service, just setting again the setOnClickPendingIntent in the OnReceive method"

I tracked the onReceive method and it is called everytime I change the orientation of my device, so I called again the configuration of my widget on the onReceive method.

Anyway Im not sure this is the best solution to solve that problem, if anyone knows a better solution please share.

Thanks.

DiegoVargasg
  • 165
  • 2
  • 7
1

Whenever you update the look of your widget (using either an Activity or your Broadcast Receiver [App widget provider]), you must also reassign all the PendingIntents for the click handlers, and then call updateAppWidget() as normal.

Example with setTextViewText():

// This will update the Widget, but cause it to
// stop working after an orientation change.
updateWidget()
{
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
remoteViews.setTextViewText(R.id.widget_text_view, "Updated widget");

appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}


// This is the correct way to update the Widget,
// so that it works after orientation change.
updateWidget()
{
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
remoteViews.setTextViewText(R.id.widget_text_view, "Updated widget");

Intent intent = new Intent(context, MyWidgetActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, ...);
remoteViews.setOnClickPendingIntent(R.id.widget_click_button, pendingIntent);

appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
Jimmy
  • 221
  • 3
  • 6