4

I have a problem with a spinner drop down list and changing orientations.

In my activity, I display a dialog in which I have two spinners. When the dialog is shown to the user and the user changes the screen orientation, everything is OK-- in my onPause, I just simply dismiss the dialog.

If the user taps on the spinner, a drop-down list is shown to user before changing orientation. But I have encountered an error when the user doesn't select anything from the drop-down list, and just leaves the list expanded before changing orientation.

I have tried many things to solve this problem (i.e. spinner.setSelection(0) in onSaveInstanceState(), etc.). My idea is to close the drop-down list from the code when the user doesn't select item and an orientation change occurs.

The error is about Android.WindowLeaked:

E/WindowManager( 2455): Activity pl.myapp.WebViewActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@45041e60 that was originally added here
E/WindowManager( 2455): android.view.WindowLeaked: Activity pl.myapp.WebViewActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@45041e60 that was originally added here
E/WindowManager( 2455):     at android.view.ViewRoot.<init>(ViewRoot.java:247)
E/WindowManager( 2455):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
E/WindowManager( 2455):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
E/WindowManager( 2455):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
E/WindowManager( 2455):     at android.app.Dialog.show(Dialog.java:241)
E/WindowManager( 2455):     at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
E/WindowManager( 2455):     at android.widget.Spinner.performClick(Spinner.java:257)
E/WindowManager( 2455):     at android.view.View$PerformClick.run(View.java:8816)
E/WindowManager( 2455):     at android.os.Handler.handleCallback(Handler.java:587)
E/WindowManager( 2455):     at android.os.Handler.dispatchMessage(Handler.java:92)
E/WindowManager( 2455):     at android.os.Looper.loop(Looper.java:123)
E/WindowManager( 2455):     at android.app.ActivityThread.main(ActivityThread.java:4627)
E/WindowManager( 2455):     at java.lang.reflect.Method.invokeNative(Native Method)
E/WindowManager( 2455):     at java.lang.reflect.Method.invoke(Method.java:521)
E/WindowManager( 2455):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/WindowManager( 2455):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/WindowManager( 2455):     at dalvik.system.NativeStart.main(Native Method)

Also, this error appears:

D/AndroidRuntime( 2455): Shutting down VM
W/dalvikvm( 2455): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
E/AndroidRuntime( 2455): FATAL EXCEPTION: main
E/AndroidRuntime( 2455): java.lang.IllegalArgumentException: View not attached to window manager
E/AndroidRuntime( 2455):    at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:355)
E/AndroidRuntime( 2455):    at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:200)
E/AndroidRuntime( 2455):    at android.view.Window$LocalWindowManager.removeView(Window.java:432)
E/AndroidRuntime( 2455):    at android.app.Dialog.dismissDialog(Dialog.java:278)
E/AndroidRuntime( 2455):    at android.app.Dialog.access$000(Dialog.java:71)
E/AndroidRuntime( 2455):    at android.app.Dialog$1.run(Dialog.java:111)
E/AndroidRuntime( 2455):    at android.app.Dialog.dismiss(Dialog.java:268)
E/AndroidRuntime( 2455):    at android.widget.Spinner.onDetachedFromWindow(Spinner.java:86)
E/AndroidRuntime( 2455):    at android.view.View.dispatchDetachedFromWindow(View.java:6033)
E/AndroidRuntime( 2455):    at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1158)
E/AndroidRuntime( 2455):    at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1156)
E/AndroidRuntime( 2455):    at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1156)
E/AndroidRuntime( 2455):    at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1156)
E/AndroidRuntime( 2455):    at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1156)
E/AndroidRuntime( 2455):    at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1156)
E/AndroidRuntime( 2455):    at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1156)
E/AndroidRuntime( 2455):    at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1156)
E/AndroidRuntime( 2455):    at android.view.ViewRoot.dispatchDetachedFromWindow(ViewRoot.java:1630)
E/AndroidRuntime( 2455):    at android.view.ViewRoot.doDie(ViewRoot.java:2671)
E/AndroidRuntime( 2455):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1948)
E/AndroidRuntime( 2455):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 2455):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 2455):    at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime( 2455):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 2455):    at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 2455):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime( 2455):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime( 2455):    at dalvik.system.NativeStart.main(Native Method)

EDIT

SOLUTION :

Finally I found what cause this *&#$% error. In manifest in my Activity I have android:configChanges="orientation" , when I change it to android:configChanges="keyboardHidden|orientation" problem is gone !

Thx 4 help !

Bandzio
  • 617
  • 2
  • 10
  • 21

3 Answers3

1

I had an idea to prevent orientation changes when dialog with spinner is displayed, but found that there is no reliable way to do that. The best solution that I found is to replace spinner with a button.

Community
  • 1
  • 1
Juozas Kontvainis
  • 9,461
  • 6
  • 55
  • 66
  • It's true, it's not the best way. See the note here http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange and save and restore the state yourself . Most of the time, you'll want to do what they explain at the top of that page: "To properly handle a restart, it is important that your activity restores its previous state through the normal Activity lifecycle, in which Android calls onSaveInstanceState() before it destroys your activity so that you can save data about the application state. You can then restore the state during onCreate() or onRestoreI..." – Scott Merritt Aug 28 '13 at 15:37
0

This occurs because onDetachedFromWindow() does not get called on the spinner when dialog.dismiss() is called. The solution for this is to create a simple custom spinner class the exposes this method.

 public class DialogSpinner extends Spinner {

     public DialogSpinner(Context context, AttributeSet attrs) {
         super(context, attrs);
     }

     @Override
     public void onDetachedFromWindow() {
         super.onDetachedFromWindow();
     }
 }

Now the onDetachedFromWindow() method is publicly available. Then in your dialog class override the onPause() method.

 @Override
 public void onPause() {
     mYourSpinner.onDetachedFromWindow();
     super.onPause();
 }

It's definately a workaround, but it seems to do the trick.

djbrick
  • 213
  • 1
  • 4
0

The answer is here: app crashes when alert dialog is open and phone(emulator) changes its orientation . Look at answer from MegaMind to help resolve this.

Community
  • 1
  • 1
eyespyus
  • 1,576
  • 19
  • 20
  • 1
    Unfortunately it doesn't work for me. I changed my dialog precedure to run via onCreateDialog. I dismissDialog(xxx) in onPause and it is still throw error while spinner has open drop down list when orientation changed. – Bandzio Mar 09 '11 at 08:39