谷歌建议我们使用DialogFragment而不是使用Fragments API创建一个简单的对话框,但是使用一个孤立的DialogFragment来创建一个简单的Yes-No确认消息框是荒谬的。在这种情况下,最佳实践是什么?


当前回答

在AlertDialog上使用DialogFragment:


Since the introduction of API level 13: the showDialog method from Activity is deprecated. Invoking a dialog elsewhere in code is not advisable since you will have to manage the the dialog yourself (e.g. orientation change). Difference DialogFragment - AlertDialog Are they so much different? From Android reference regarding DialogFragment: A DialogFragment is a fragment that displays a dialog window, floating on top of its activity's window. This fragment contains a Dialog object, which it displays as appropriate based on the fragment's state. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog. Other notes Fragments are a natural evolution in the Android framework due to the diversity of devices with different screen sizes. DialogFragments and Fragments are made available in the support library which makes the class usable in all current used versions of Android.

其他回答

你可以创建一般的DialogFragment子类,如YesNoDialog和OkDialog,并传入标题和消息,如果你在应用程序中使用对话框很多。

public class YesNoDialog extends DialogFragment
{
    public static final String ARG_TITLE = "YesNoDialog.Title";
    public static final String ARG_MESSAGE = "YesNoDialog.Message";

    public YesNoDialog()
    {

    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        Bundle args = getArguments();
        String title = args.getString(ARG_TITLE);
        String message = args.getString(ARG_MESSAGE);

        return new AlertDialog.Builder(getActivity())
            .setTitle(title)
            .setMessage(message)
            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which)
                {
                    getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, null);
                }
            })
            .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which)
                {
                    getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_CANCELED, null);
                }
            })
            .create();
    }
}

然后使用以下命令调用它:

    DialogFragment dialog = new YesNoDialog();
    Bundle args = new Bundle();
    args.putString(YesNoDialog.ARG_TITLE, title);
    args.putString(YesNoDialog.ARG_MESSAGE, message);
    dialog.setArguments(args);
    dialog.setTargetFragment(this, YES_NO_CALL);
    dialog.show(getFragmentManager(), "tag");

并在onActivityResult中处理结果。

使用Dialog进行简单的是或否对话框。

当你需要更复杂的视图,其中你需要获得生命周期,如oncreate,请求权限,任何生命周期覆盖我将使用一个对话框片段。因此,您可以分离权限和对话框需要操作的任何其他代码,而不必与调用活动通信。

DialogFragment具有对话框和片段的功能。基本上所有的生命周期事件都是由DialogFragment自动管理的,比如屏幕配置的改变等。

DialogFragment基本上是一个可以用作对话框的片段。

使用DialogFragment而不是Dialog的原因如下: 在配置更改和保存&恢复流程后,自动重新创建DialogFragment DialogFragment继承了完整的Fragment生命周期 不再有illegalstateexception和泄漏的窗口崩溃。当活动被警报对话框破坏时,这是非常常见的 还在那里。

更详细地

在AlertDialog上使用DialogFragment:


Since the introduction of API level 13: the showDialog method from Activity is deprecated. Invoking a dialog elsewhere in code is not advisable since you will have to manage the the dialog yourself (e.g. orientation change). Difference DialogFragment - AlertDialog Are they so much different? From Android reference regarding DialogFragment: A DialogFragment is a fragment that displays a dialog window, floating on top of its activity's window. This fragment contains a Dialog object, which it displays as appropriate based on the fragment's state. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog. Other notes Fragments are a natural evolution in the Android framework due to the diversity of devices with different screen sizes. DialogFragments and Fragments are made available in the support library which makes the class usable in all current used versions of Android.