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())
fromonCreateDialog(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?
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.
According to issues from link of Blackbelt we should return AppCompatDialog and use supportRequestWindowFeature(). I propose change like this:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AppCompatDialog dialog = new AppCompatDialog(getActivity(), getTheme());
return dialog;
}
@Override
public void setupDialog(Dialog dialog, int style) {
switch (style) {
case STYLE_NO_INPUT:
dialog.getWindow().addFlags(
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
// fall through...
case STYLE_NO_FRAME:
case STYLE_NO_TITLE:
((AppCompatDialog) dialog).supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
}
}
or better, try not to touch original code:
@Override
public void setupDialog(Dialog dialog, int style) {
super.setupDialog(dialog,style);
if(style == STYLE_NO_TITLE)
((AppCompatDialog) dialog).supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With