在尝试清理现有项目中的代码并删除有关调用showDialog()
Activity 的警告时,我已将一系列按顺序显示的相关对话框移动到新的DialogFragment
类中。它工作正常,但是当我在给定对话框上按下取消按钮时,它总是带我回到前一个对话框(保存到后台堆栈),而我希望取消按钮关闭所有对话框并返回到主要活动中的视图。
按下后退按钮应继续返回到后堆栈上的上一个对话框。
这是我当前的代码,我已将其简化为仅包含两个对话框,尽管在这两个之间的链中还有几个对话框,它们在import()
调用之前出现在我的实际应用程序中。
public class ImportDialog extends DialogFragment {
private int mType = 0;
public static final int DIALOG_IMPORT_HINT = 0;
// ... several more
public static final int DIALOG_IMPORT = 8;
public interface ImportDialogListener {
public void import();
public void showImportDialog(int type);
public void dismissAllDialogFragments();
}
/**
* A set of dialogs which deal with importing a file
*
* @param dialogType An integer which specifies which of the sub-dialogs to show
*/
public static ImportDialog newInstance(int dialogType) {
ImportDialog f = new ImportDialog();
Bundle args = new Bundle();
args.putInt("dialogType", dialogType);
f.setArguments(args);
return f;
}
@Override
public AlertDialog onCreateDialog(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mType = getArguments().getInt("dialogType");
Resources res = getResources();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
setCancelable(true);
switch (mType) {
case DIALOG_IMPORT_HINT:
// First dialog
builder.setTitle("Title");
builder.setMessage("Message");
builder.setPositiveButton(res.getString(R.string.dialog_ok), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
((ImportDialogListener) getActivity()).showImportDialog(DIALOG_IMPORT_SELECT);
}
});
builder.setNegativeButton(res.getString(R.string.dialog_cancel), mNegativeClickListener);
return builder.create();
// ... several more
case DIALOG_IMPORT:
// Second dialog
builder.setTitle("Different title");
builder.setMessage("Different message");
builder.setPositiveButton(res.getString(R.string.import_message_add),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
((ImportDialogListener) getActivity()).import();
}
});
builder.setNegativeButton(res.getString(R.string.dialog_cancel), mNegativeClickListener);
builder.setCancelable(true);
return builder.create();
default:
return null;
}
}
private DialogInterface.OnClickListener mNegativeClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
((ImportDialogListener) getActivity()).dismissAllDialogFragments();
}
};
}
然后在主Activity中我实现ImportDialogListener
接口如下:
public void showImportDialog(int type) {
// DialogFragment.show() will take care of adding the fragment
// in a transaction. We also want to remove any currently showing
// dialog, so make our own transaction and take care of that here.
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
ft.remove(prev);
}
// save transaction to the back stack (input argument is optional name) so it can be reversed
ft.addToBackStack(null);
DialogFragment newFragment = ImportDialog.newInstance(type);
newFragment.show(ft, "dialog");
}
public void import() {
// Do stuff
}
// Dismiss whatever dialog is showing... THIS IS THE PART THAT DOESN'T WORK
public void dismissAllDialogFragments() {
getSupportFragmentManager().popBackStack("dialog", FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
DialogFragment df = (DialogFragment) prev;
df.dismiss();
}
}
我怎样才能让它工作,我在这里做一些根本错误的事情吗?