app crashes when alert dialog is open and phone(emulator) changes its orientation-Collection of common programming errors

guys I’ve been searching and studying about this for a whole day. The app crashes not only when a dialog is open, but also when a spinner is open (and I’m sure it would be bunch of other scenarios that I haven’t come across).

choosing between onRetainNonConfigurationInstance() and onConfigurationChanged(Configuration newConfig) won’t help me. (maybe I’m wrong) – actually I prefer to use first one.

BUT I want to ask: for a single open dialog box or open spinner do I have to create everything from scratch? is this a way? sometimes it’s worthless to reinitialize a big class with lot of widgets inside just for a time that maybe user changes the orientation.

plz give me hints and guidance. Thanks in advance.

Edited :::

Error code:

01-25 17:56:48.182: ERROR/WindowManager(312): Activity XXX has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@43e63058 that was originally added here
01-25 17:56:48.182: ERROR/WindowManager(312): android.view.WindowLeaked: Activity XXX has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@43e63058 that 
was originally added here
01-25 17:56:48.182: ERROR/WindowManager(312):     at android.view.ViewRoot.(ViewRoot.java:247)
01-25 17:56:48.182: ERROR/WindowManager(312):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
01-25 17:56:48.182: ERROR/WindowManager(312):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
01-25 17:56:48.182: ERROR/WindowManager(312):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
01-25 17:56:48.182: ERROR/WindowManager(312):     at android.app.Dialog.show(Dialog.java:241)
01-25 17:56:48.182: ERROR/WindowManager(312):     at XXX.menuGroupDialog(XXX.java:946)
0    1-25 17:56:48.182: ERROR/WindowManager(312):     at XXX.onOptionsItemSelected
(GroupManagement.java:257)
01-25 17:56:48.182: ERROR/WindowManager(312):     at android.app.Activity.onMenuItemSelected(Activity.java:2195)01-25 17:56:48.182: ERROR/WindowManager(312):     at 
com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:730)
01-25 17:56:48.182: ERROR/WindowManager(312):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)
01-25 17:56:48.182: ERROR/WindowManager(312):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
01-25 17:56:48.182: 
ERROR/WindowManager(312):     at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:532)
01-25 17:56:48.182: ERROR/WindowManager(312):     at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
01-25 17:56:48.182: ERROR/WindowManager(312):     at android.view.View$PerformClick.run(View.java:8816)
01-25 17:56:48.182: ERROR/WindowManager(312):     at 
android.os.Handler.handleCallback(Handler.java:587)
01-25 17:56:48.182: ERROR/WindowManager(312):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-25 17:56:48.182: ERROR/WindowManager(312):     at android.os.Looper.loop(Looper.java:123)
01-25 17:56:48.182: ERROR/WindowManager(312):     at 
android.app.ActivityThread.main(ActivityThread.java:4627)
01-25 17:56:48.182: ERROR/WindowManager(312):     at java.lang.reflect.Method.invokeNative(Native Method)
01-25 17:56:48.182: ERROR/WindowManager(312):     at java.lang.reflect.Method.invoke(Method.java:521)
01-25 17:56:48.182: ERROR/WindowManager(312):     at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-25 17:56:48.182: ERROR/WindowManager(312):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-25 17:56:48.182: ERROR/WindowManager(312):     at 
dalvik.system.NativeStart.main(Native Method)

I know that this is from calling dismiss() function. but how can I handle orientation change when having dialog open?

  1. Are you using progressDialog.show() and progressbarDialog.dismiss() in the AyncTask?

    If yes try using showDialog(id) and dismissDialog(id) in the AyncTask instead of it. You will have to write custom dialog in the onCreateDialog() for showing the loading dialog. showDialog and dismissDialog methods are activity level methods. I mean they are invoked on the Activity or its Context. So even if the activity is recreated after orientation change, latest context will be made available for the Dialog.

    check for Sample code here. Modify DIALOG_PROGRESS case in it as per your requirement.

    Also, onRetainNonConfigurationInstance() works perfectly with the orientation change. It just that due to window leak problem you are not able to see it.

  2. By default your Activity is destroyed and recreated when the orientation changes. You can turn this off, and handing this configuration change on your own by setting the configChanges attribute.

  3. You can either disable configuration changes, that is to say, you can stop the application from trying to redraw once the orientation of the phone changes by using XML in your Manifest file:

    
    

    When the orientation of your phone changes, by default Android will try and re-instantiate your page.