DialogFragment 的奇怪行为

DialogFragment's weird behavior

我花了一整天的时间试图弥补这一点,但我做不到..

这就是问题所在:我想要一个 yes/no AlertDialog 在方向改变时不会消失的,所以我决定使用 DialogFragment.

所以我准备了代码和第一次使用,一切都很完美,但是如果我按下按钮(应该显示对话框)再次(第二次、第三次和更多次)对话框没有出现!虽然我可以从日志中看到它实际上创建了实例并且我没有错误,它就在那里,我只是看不到它!

如果我折叠应用程序,或关闭/在屏幕上(我相信这是关于调用 onResume() 方法)对话框出现,所有这些(取决于我花了多少时间点击按钮),似乎是一些显示问题或刷新问题。我不知道,所以我来这里希望得到一些帮助。

关于我的代码:

我有一个 ListView 自定义适配器 ,在那个适配器中我有代码来显示 AlertDialog (DialogFragment) - 作为 ImageButton onClickListener 的一部分。

我使用的DialogFragment代码:

  public static class cMyDialogFragment extends DialogFragment {

    public static cMyDialogFragment newInstance(int title) {
        cMyDialogFragment frag = new cMyDialogFragment();
        Bundle args = new Bundle();
        args.putInt("title", title);
        frag.setArguments(args);
        return frag;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        int title = getArguments().getInt("title");
        this.setCancelable(true);
        setRetainInstance(true);

        return new AlertDialog.Builder(getActivity())
                //   .setIcon(R.drawable.alert_dialog_icon)
                .setTitle(title)
                .setPositiveButton(R.string.yes,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                ((ActAudiorecords) getActivity()).doPositiveClick();
                            }
                        }
                )
                .setNegativeButton(R.string.no,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                ((ActAudiorecords) getActivity()).doNegativeClick();
                            }
                        }
                )
                .create();
    }
}

调用对话框显示的代码(在自定义 ListView 适配器中):

   public View getView(final int position, View convertView, ViewGroup parent) {

        View vi = convertView;
        if (vi == null)
            vi = inflater.inflate(R.layout.recordings_row, null);

        TextView tvDate = (TextView) vi.findViewById(R.id.tv_Recordings_r_date);
        tvDate.setText(ainfo.get(position).getDate());

        ImageButton ibtn_play = (ImageButton) vi.findViewById(R.id.ibtnPlay);
        final String localPath = dPath + File.separator + ainfo.get(position).getFName();

        ImageButton ibtn_remove = (ImageButton) vi.findViewById(R.id.ibtnRecordings_r_remove);
        ibtn_remove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                curFName = ainfo.get(position).getFName();
                curID = ainfo.get(position).getID();

                showDialog();
            }
        });


        ibtn_play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                play(localPath);
            }
        });

        return vi;
    }

附加功能:

  void showDialog() {
    DialogFragment newFragment = cMyDialogFragment.newInstance(
            R.string.do_you_want_to_remove_the_file);
    newFragment.show(getFragmentManager(), "dialog");
}

public void doPositiveClick() {
    // Do stuff here.
    ps_db.delete(const_audiorecords_tname, "id = " + curID, null);
    new File(dPath + File.separator + curFName).delete();
    Toast.makeText(ActAudiorecords.this, getString(R.string.audiorecord_has_been_removed), Toast.LENGTH_LONG).show();
    ActAudiorecords.this.onCreate(null); //Restarting the Activity to refresh the LV

    Log.i("FragmentAlertDialog", "Positive click!");
}

public void doNegativeClick() {
    // Do stuff here.
    Toast.makeText(ActAudiorecords.this, getString(R.string.the_operation_has_been_cancelled), Toast.LENGTH_LONG).show();

    Log.i("FragmentAlertDialog", "Negative click!");
}

这一切都是因为这条线:

  ActAudiorecords.this.onCreate(null);

所以在用 null 作为 savedInstance 调用 onCreate() 之后,它一直在将 link 删除到 DialogFragment(据我所知),这是用于刷新的行Activity、我通过拆分 onCreate() 中的代码解决了这个问题,应该只调用一次(在 Activity 开始)和部分应该在每个刷新点(例如 GUI 设置等)调用。

我相信我也可以保存当前的 Bundle 并将其传递给 onCreate() 而不是 null 并且它会像现在一样工作,但我认为调用函数比调用 onCreate( )翻来覆去,就这样了,谢谢大家的帮助。