I just thought I would talk a bit about how I solved an annoying problem with my DSI: Miami widget since I had a really hard time finding a solution online. The problem would show up every time the home screen would change orientations (from portrait to landscape and vice versa). If there was an orientation change, the widget would freeze and the button/text box wouldn’t respond anymore. This drove me nuts! I finally solve the stupid problem today. This is what was happening:
-I have an IntentService class called UpdateService which basically throws a PendingIntent out there to listen for the button press. Here’s the code that is in it’s buildUpdate method:
private RemoteViews buildUpdate(Context context) {
RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.widget_message);
Intent i = new Intent(this, DSIMiamiWidget.class);
i.setAction(REFRESH_QUOTE);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
updateViews.setOnClickPendingIntent(R.id.WidgetButton, pi);
return (updateViews);
}
-This code was fine, actually. It worked great when the home screen stayed in the orientation in which the widget was born.
-The problem was when the button was pressed, it called a changeText() method which updated the text box, requiring me to do a new RemoteViews update. Here’s the gist of the code:
public void changeText(Context context)
{
RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.widget_message);
// int textSize;
if (!randomQuotes.isEmpty())
updateViews.setTextViewText(R.id.WidgetTextView, randomQuotes.get(0));
else
{
if (quotes == null)
this.quotes = context.getResources().getStringArray(R.array.quotes);
convertArrayToList();
randomizeQuotes();
updateViews.setTextViewText(R.id.WidgetTextView, randomQuotes.get(0));
}
randomQuotes.remove(0);
ComponentName thisWidget = new ComponentName(context, DSIMiamiWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(thisWidget, updateViews);
}
-Well, I didn’t realize that I had to REDO all the PendingIntent stuff after doing that, too. So, I threw this in right before “ComponentName thisWidget = new ComponentName…” at the bottom:
Intent i = new Intent(context, this.getClass());
i.setAction(REFRESH_QUOTE);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
updateViews.setOnClickPendingIntent(R.id.WidgetButton, pi);
TADAAAAAAAAAA. That was a pain…