lorensiuswlt/NewQuickAction

'activity has leaked window' exception on change orientation

ardock opened this issue · 5 comments

Hi Lorensius,

Android recreates the view on change orientation but dont manage it for us.

If you open the quickaction and changes orientation, you ll see an 'activity leaked a window' exception.

Log:
10-25 04:30:47.686: E/WindowManager(221): Activity test has leaked window android.widget.PopupWindow$PopupViewContainer@44e0ab60 that was originally added here
10-25 04:30:47.686: E/WindowManager(221): android.view.WindowLeaked: Activity test has leaked window android.widget.PopupWindow$PopupViewContainer@44e0ab60 that was originally added here
10-25 04:30:47.686: E/WindowManager(221): at android.view.ViewRoot.(ViewRoot.java:227)
10-25 04:30:47.686: E/WindowManager(221): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
10-25 04:30:47.686: E/WindowManager(221): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
10-25 04:30:47.686: E/WindowManager(221): at android.view.Window$LocalWindowManager.addView(Window.java:424)

A possible solution, dismiss the quickaction:
Mark Murphy said "Move that logic out of the the View and into the Activity. Then, dismiss
them in whichever of onPause/onStop/onDestroy fits your needs best. Save a
flag or something in onSaveInstanceState() to let you know to re-open them
after the orientation change."
http://comments.gmane.org/gmane.comp.handhelds.android.devel/60167

A better explanation of the problem and the two possible solutions:

Avoid dialog leak issue:
http://androiddev.orkitra.com/?p=111

Handle configuration or orientation change:
http://androiddev.orkitra.com/?p=108

I just try the onConfigurationChanged method and it solves the problem.

Use the onConfigurationChanged method of Activity. See the following code:

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

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    }
}

You also have to edit the appropriate element in your manifest file to include the android:configChanges

Just see the code below:

<activity android:name=".MyActivity"
      android:configChanges="orientation|keyboardHidden"
      android:label="@string/app_name" />

http://stackoverflow.com/questions/5726657/how-to-detect-orientation-change-in-layout-in-android#

Hi ardock, thanks for reporting the issue and the solution. I just tested it on android 2.3 and didnt get the problem. On what Android version did u test it?

Thanks for the quickaction. I tested on android 2.1.

I tested your NewQuickAction and NewQuickAction3d sources on android 2.1 emulator and i got the problem.
You just need to open the quickaction and change orientation and you ll see an 'activity leaked a window' exception in log and the quickaction dissapear but the app dont crash.

NOTE: with Android 3.2 (API level 13) or higher , the "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher , you must decalare android:configChanges="orientation|screenSize" for API level 13 or higher .