0
votes

I have a simple SettingsActivity that holds a preference fragment. This fragment displays a button that opens a list dialog. The view and the dialog are created automatically by providing a preferences.xml file.

public class SettingsActivity extends FragmentActivity {

    private static final String TAG_SETTINGS_FRAGMENT = "settings_fragment";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .replace(android.R.id.content, new SettingsFragment(), TAG_SETTINGS_FRAGMENT).commit();
        }
    }

    public static class SettingsFragment extends PreferenceFragment{

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // init settings here
            setRetainInstance(true);
        }
    }
}

So everything works fine, except when I rotate the screen while the dialog is present. On rotation the dialog disappears. When I show it again I get following exception:

E/WindowManager﹕ android.view.WindowLeaked: Activity SettingsActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42300a98 V.E..... R....... 0,0-729,1134} that was originally added here at android.view.ViewRootImpl.(ViewRootImpl.java:346) at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) at android.app.Dialog.show(Dialog.java:286) at android.preference.DialogPreference.showDialog(DialogPreference.java:308) at android.preference.DialogPreference.onClick(DialogPreference.java:266) at android.preference.Preference.performClick(Preference.java:952) at android.preference.PreferenceScreen.onItemClick(PreferenceScreen.java:215)

After this I destroy the activity by moving back and instantly get this exception:

java.lang.RuntimeException: Unable to destroy activity {SettingsActivity}: java.lang.IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView{423fc108 V.E..... R....... 0,0-729,1134} not attached to window manager

Any ideas how to avoid this?

2
Where do you show the dialog? Because it's usually a dialog thats attempting to display after an activity has been closed (although it can be presented for other reasons also). The "Leaked Window" message, I mean. - fantasitcalbeastly
@LokiSinclair Not me is creating the dialog but the PreferenceFragment. I just provide a xml preference file and the PreferenceFragment manages displaying/hiding the dialog. - Upvote

2 Answers

1
votes

Try to remove setRetainInstance(true).

I had the same problem and it disappeared when I stopped retaining the fragment.

0
votes

Why don't you create the fragment anyways without check the savedInstance?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment(), TAG_SETTINGS_FRAGMENT).commit();
}