从片段对话显示片段

时间:2011-11-02 07:08:21

标签: android android-fragments android-alertdialog

在我的代码中,我试图在FragmentDialog的某些情况下显示Fragment,但没有任何反应。 这是我的代码:

public Dialog onCreateDialog(Bundle bundle) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle(getString(R.string.trip_type_title));
    builder.setItems(
            new String[]{
                    getString(R.string.option1),
                    getString(R.string.option2),
                    getString(R.string.cancel)
            },
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {
                    switch (item) {
                        case 0:
                            break;
                        case 1:
                            FragmentManager manager = getFragmentManager();
                            FragmentTransaction transaction = manager.beginTransaction();

                            Fragment fragment = new SomeFragment();

                            transaction.replace(R.id.content, fragment);
                            transaction.addToBackStack(null);

                            break;
                        case 2:
                            break;
                    }
                }
            });


    AlertDialog dialog = builder.create();

    return dialog;
}

我做错了什么?

1 个答案:

答案 0 :(得分:2)

尝试提交更改:

transaction.replace(R.id.content, fragment);
transaction.addToBackStack(null);
transaction.commit();