Landscape Progress Madness !!!

java.lang.IllegalArgumentException: View not attached to window manager

This was the first and only error reported for our android client for cinemaat.com which now has over 500 downloads. I scratch my head looking at the stack trace trying to reproduce the bug! The exception is thrown at a line that dismisses the progress dialog  indicating that movie data was loaded and the application is ready to be used. No matter how absurd scenarios I try the application is resilient, nothing breaks!! Finally, I was reading about the emulator I tried flipping the phone (Ctrl+F12), ok the UI wasn’t designed to work on landscape so it looked  ugly!!  I tried doing this a couple of times and finally I can tell when this happens, It happens when you flip the screen while there is a dialog in progress. Ok so the solution came easy for me, the application wasn’t designed for landscape mode any way so I just disabled that. You can do this by editing your AndroidManifest.xml and slapping a:

android:screenOrientation=”portrait”

on the activities you want to disable the mode on.

However, I started thinking, what if I wanted to keep my landscape mode and avoid this bug, so I created a test project to do this and reached the following conclusion:

  1. Have a flag to mark loading complete instead of checking on a dialog reference.
  2. override the onSaveInstanceState() method and have it dismiss the dialog if it exists.
  3. In the onCreate() always check for the flag and not for the dialog reference.

There is another method that I am yet to try but should work theoretically, registering your activity to listen on configChanges orientation should alter the lifecycle of your activity and remove the destroy-create behavior and instead invoke the onConfigurationChanged() method. I am yet to try this though, will do so then update the post one day.

Bookmark and Share

Leave a Reply