Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

After orientation change buttons on a widget are not responding [duplicate]

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.

like image 951
Alex Avatar asked Jan 01 '11 12:01

Alex


3 Answers

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.

like image 143
Martin Stone Avatar answered Oct 22 '22 22:10

Martin Stone


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.

like image 31
DiegoVargasg Avatar answered Oct 22 '22 23:10

DiegoVargasg


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);
}
like image 1
Jimmy Avatar answered Oct 22 '22 21:10

Jimmy