in my app when I have a dialog open and the device rotates, the app crashes.

I thought I was already doing everything to handle rotations myself

In all my activities I have this enabled in the manifest

android:configChanges="keyboardHidden|orientation"
        android:screenOrientation="fullSensor" 

and for the dialogs in question I have this code

  if(!WebViewActivity.this.isFinishing() && dialog != null && dialog.isShowing()){
                dialog.dismiss();
  }

yet, I still get this crash message if the dialog is open while a device rotates

 java.lang.IllegalArgumentException: View not attached to window manager
at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:383)
at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:285)
at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:79)
at android.app.Dialog.dismissDialog(Dialog.java:323)
at android.app.Dialog.dismiss(Dialog.java:306)
at bundle.android.views.WebViewActivity$1.onPageFinished(WebViewActivity.java:133)
at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:323)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5039)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)

or this error message

  android.view.WindowManager$BadTokenException: Unable to add window -- token  android.os.BinderProxy@42173a50 is not valid; is your activity running?

This example is about an activity named WebViewActivity but I have many other activities with the same problem

I didn't feel like there was a need to use the onConfigurationChanged lifecycle method, but any solution would be welcome!

有帮助吗?

解决方案

the answer is to use

android:configChanges="keyboardHidden|orientation"
android:screenOrientation="fullSensor"

like I was using, along with an extra parameter screenSize for android api level 13+

android:configChanges="keyboardHidden|orientation|screenSize"
android:screenOrientation="fullSensor"

this has to be done for each activity in the manifest where the dialogs may be an issue, as well as putting in

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top