我无法弄清楚为什么按下硬件后退按钮(或对话框的OK按钮)有时不会将我返回到初始的DialogFragment。
我有一个DeductionListDialog片段,可以从MainActivity的选项菜单中调用:
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;
}
});
当listview onItemClick&amp;单击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
谢谢大家。