Home > android, ui > Widgets and orientation changes

Widgets and orientation changes

The widget in WiFi Manager initially didn’t work correctly on the Moto Milestone after orientation changes (fixed in 1.3 and later).

The main problem was: after sliding out the keyboard, widget views were reset to their original state (as in the layout file). The widget didn’t respond to clicks, either.

This is what I found out while fixing this issue:

My Investigations

  • When orientation changes, it’s not possible for AppWidgetProvider to be notified. onUpdate is not called, and neither is any other notification method.
  • The widget’s “real” views hosted in home screen process are destroyed and recreated (as uaual, from xml layout file).
  • Android correctly uses appropriate -land or -port layout for the previous step.
  • After the layout is inflated, Android uses the most recent RemoteViews object (which it saves somewhere) to bring your widget up to date.

My recommendations

  • Create separate layouts for -port and -land orientations.
  • Orientation-dependent data presentation differences should be handled by your widget’s layouts (i.e. TextView ellipsize), not on the AppWidgetProvider side (i.e. measuring and trimming text)
  • This is the most important: make sure each RemoteViews object you push to the widget completely specifies widget state, including states of all views and all PendingIntent‘s.
Advertisements
Categories: android, ui
  1. February 6, 2012 at 3:20 pm

    can you please explain “This is the most important: make sure each RemoteViews object you push to the widget completely specifies widget state, including states of all views and all PendingIntent‘s ” with some example.

    • February 7, 2012 at 8:31 pm

      Hi. What it means is:

      Don’t first send a RemoteViews that sets up the background, then a RemoteViews with PendingIntent objects, then a RemoteViews with some current text or images.

      Each and every RemoteViews object that you send to the widget absolutely must have all the things you want to end up there: background, pending intents, text, images.

      In other words, under certain conditions, only the most recent RemoteViews object is saved and applied to a fresh copy of your layout.

      Don’t assume the widget maintains state from one RemoteViews update to another.

  2. sasha
    January 21, 2012 at 5:58 am

    Thanks for the tip, Konstantin. I was scratching my head, trying to figure out where my clicks have gone to 🙂

  3. July 6, 2010 at 10:59 am

    A typo that confused me for a moment, here and in your note to the Android Dev group — ‘RemoteViews’, not ‘RemoveViews’.

    Thanks for clarifying how this works.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s