Android 如何消除DialogFragment而不返回堆栈中的最后一项
在尝试清理现有项目中的代码并删除有关在活动中调用Android 如何消除DialogFragment而不返回堆栈中的最后一项,android,dialogfragment,Android,Dialogfragment,在尝试清理现有项目中的代码并删除有关在活动中调用showDialog()的警告时,我已将一组按顺序显示的相关对话框移动到一个新的DialogFragment类中。它工作正常,但是当我按下给定对话框上的“取消”按钮时,它总是将我带回上一个对话框(保存到后堆栈),而我希望“取消”按钮关闭所有对话框并返回主活动中的视图 按下“上一步”按钮应继续返回上一个对话框 这是我当前的代码,我已将其简化为只包含两个对话框,尽管在这两个对话框之间的链中还有几个对话框,它们在调用import()之前显示在我的实际应用
showDialog()
的警告时,我已将一组按顺序显示的相关对话框移动到一个新的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();
}
};
}
然后在主要活动中,我实现了importDiaglistener
界面,如下所示:
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();
}
}
我怎样才能让它工作,我在这里做了一些根本错误的事情吗 将事务添加到后台堆栈时,请提供后台堆栈状态的名称。e、 g.对话 改变
ft.addToBackStack(null);
到
要从后台堆栈中删除与后台堆栈状态名称匹配的条目,请使用。这将从顶部移除所有具有名称对话框的后堆栈状态,直到到达堆栈底部或到达具有不同名称的后堆栈状态条目。无需在对话框上显式调用Disclose
public void dismissAllDialogFragments() {
getSupportFragmentManager().popBackStack("dialog", FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
public void dismissAllDialogFragments() {
getSupportFragmentManager().popBackStack("dialog", FragmentManager.POP_BACK_STACK_INCLUSIVE);
}