1

我不知道为什么有时按下硬件后退按钮(或对话框 OK 按钮)不会让我回到初始的 DialogFragment。

我有一个从 MainActivity 的选项菜单调用的 DeductionListDialog 片段,如下所示:

    public boolean onOptionsItemSelected(MenuItem item) {
    ...
    case R.id.action_deduction_list:
        DialogFragment newFragment = new DeductionListDialog();
        newFragment.show(getFragmentManager(), "dialog");
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }

然后,DeductionListDialog 在其 onCreateDialog 方法中有几个 onClickListener:

        // the listview that holds the deduction list
    ListView listview = (ListView) view.findViewById(android.R.id.list);
    listview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        // set a short click listener
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // create a new dialog fragment
            DialogFragment deduction_specifics = new DeductionSpecificsDialog();

            // / bundle database row so we can get the correct info
            // for our specific listing
            Bundle arguments = new Bundle();
            arguments.putLong("database_row", id);
            deduction_specifics.setArguments(arguments);
            deduction_specifics.show(getFragmentManager(), "dialog");
        }
    });
    // set the long click listener
    listview.setOnItemLongClickListener(new OnItemLongClickListener() {

        // on long click we want to open the edit fragment
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            DialogFragment deduction_edit = new DeductionEditFragment();
            Bundle arguments = new Bundle();
            arguments.putLong("database_id", id);
            deduction_edit.setArguments(arguments);
            deduction_edit.show(getFragmentManager(), "dialog");
            /*Intent deduction_edit_intent = new Intent(getActivity(), DeductionEditActivity.class);
            deduction_edit_intent.putExtra("database_id", id);
            startActivity(deduction_edit_intent);*/
            return true;
        }
    });

当单击列表视图 onItemClick 和 onItemLongClick 侦听器时,会弹出另一个对话框,其中包含各种信息。当我按下后退按钮或对话框的 OK 按钮时,我按预期返回到初始 DeductionListDialog 片段。

DeductionListDialog 在其 onCreateDialog 方法中也有:

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
...
        alertDialogBuilder.setView(view);
    alertDialogBuilder.setTitle("Deductions: ");
    alertDialogBuilder.setMessage("Long press to update or delete");
    alertDialogBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               // User clicked OK button
               dialog.dismiss();
           }
       });
    alertDialogBuilder.setNegativeButton("Add", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               // User clicked Add button
                DialogFragment deduction_edit = new DeductionEditFragment();
                Bundle arguments = new Bundle();
                deduction_edit.setArguments(arguments);
                deduction_edit.show(getFragmentManager(), "dialog");

               /*Intent deduction = new Intent(getActivity(), DeductionEditActivity.class);
               startActivity(deduction);*/
           }
       });

当我单击“添加”按钮时,会创建一个新的 DeductionEditFragment。当我单击返回(或对话框的取消和接受按钮)时,我希望视图返回到原始的 DeductionListDialog 片段,但单击只会导致片段关闭回到 MainActivity。

1)为什么会这样,因为我想学习如何在将来防止这种情况。
2) 解决此问题的最快方法是什么?
3)如果与#2不同,解决此问题的“正确”方法是什么?

可以在这里找到整个三个类(DeductionListDialog、DeductionEditDialog、DeductionSpecificsDialog)的 pastebin:http: //pastebin.com/AJQ6KCEN
谢谢大家。

4

0 回答 0