Search code examples
androidandroid-support-libraryandroid-dialogfragmentandroid-appcompat

DialogFragment with AppCompatDialog crashes if STYLE_NO_TITLE is set


I'm trying to use the new material-themed dialogs with fragments in AppCompat v22.1. According to Chris Banes, to do this:

Just return new AppCompatDialog(getActivity(), getTheme()) from onCreateDialog(Bundle).

Setting this up:

public class MyFragment extends DialogFragment
{
    public MyFragment() { }

    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new AppCompatDialog(getActivity(), getTheme());
    }

    ...
}

works perfectly in the normal case; the dialog is correctly themed and everything. However, when we try to show a dialog with the STYLE_NO_TITLE option:

 MyFragment fragment = new MyFragment();
 fragment.setStyle(DialogFragment.STYLE_NO_TITLE, 0);
 fragment.show(getSupportFragmentManager(), "DIALOG");

it causes the following exception and crash:

05-19 12:18:38.806  15458-15458/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.test.testdialog, PID: 15458
android.util.AndroidRuntimeException: requestFeature() must be called before adding content
    at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:302)
    at android.app.Dialog.requestWindowFeature(Dialog.java:1066)
    at android.support.v4.app.DialogFragment.getLayoutInflater(DialogFragment.java:317)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

I guess the problem might be related to DialogFragment calling requestWindowFeature() instead of supportRequestWindowFeature() (?)

Is there any workaround for this issue?


Solution

  • This was a bug in AppCompat v22.1 which was fixed in v22.1.1.

    It broke again in v23 and was fixed once more, in v23.0.1.

    It's working ok as of this writing.