从另一个DialogFragment显示DialogFragment

时间:2013-05-14 09:57:33

标签: android android-dialogfragment

您好我有一个DialogFragment,显示用户的选项列表,其中一个选项是“删除”选项,当用户按下删除选项时我想显示另一个DialogFragment作为确认,遗憾的是确认对话框没有显示。

这是我的代码

First Fragment代码

public class ContactDialogOption extends SherlockDialogFragment {

    public static final String TAG = ContactDialogOption.class.getSimpleName();

    public ContactDialogOption() {
        super();
        // TODO Auto-generated constructor stub
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        builder.setIcon(R.drawable.ic_launcher);
        builder.setTitle(R.string.options);

        builder.setItems(new String[] {

        getString(R.string.call), getString(R.string.send_message),
                getString(R.string.copy), getString(R.string.edit),
                getString(R.string.delete)

        }, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                          if(which == 4) //delete
                          {
FragmentManager mgr = getActivity().getSupportFragmentManager();
    FragmentTransaction ft = mgr.beginTransaction();
        Fragment old = mgr.findFragmentByTag("SecondFragment");
        if (old != null) {
            ft.remove(old);
        }
        ft.addToBackStack(null);


fragment.show(ft, fragmentTag);
                          }
            }
        });

        return builder.create();
    }
}

9 个答案:

答案 0 :(得分:8)

我遇到了完全相同的问题,当您尝试从DialogFragment打开Fragment时,不会发生这种情况。

我找到的唯一解决方案是修改以下调用:

fragment.show(ft, fragmentTag);

要:

fragment.show(getFragmentManager(), fragmentTag);

此解决方案的问题在于我们无法处理FragmentTransition

我不明白为什么行为与片段不同。

答案 1 :(得分:2)

我遇到了无法在第一个DialogFragment的正面和负面点击侦听器中显示另一个DialogFragment的相同问题。我的解决方案是立即弹出第一个片段,它允许第二个DialogFragment成功附加和显示。

// Call this before adding the second dialog fragment activity.getSupportFragmentManager().popBackStackImmediate();

答案 2 :(得分:2)

请检查以下代码。希望这会对你们中的许多人有所帮助!

public class SubcategoryFragment extends DialogFragment {

public SubcategoryFragment() {

}

public static SubcategoryFragment newInstance(Integer code, String name) {
    SubcategoryFragment fragment = new SubcategoryFragment();

    mCode = code;
    mTitle = name;
    return fragment;
}


@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    liststring = new ArrayList<>();

    getAdapter();

}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_subcategory, container, false);
    gridView = (GridView) view.findViewById(R.id.sub_grid);


    return view;

}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    closeDialog = (ImageView) getDialog().findViewById(R.id.closeDialog);
    title = (TextView) getDialog().findViewById(R.id.dialogTitle);
    gridView = (GridView) getDialog().findViewById(R.id.sub_grid);

    title.setText(String.format("Choose %s", mTitle));
    closeDialog.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getDialog().dismiss();
        }
    });


}

@Override
public Dialog onCreateDialog(@NonNull Bundle savedInstanceState) {
    Dialog dialog = super.onCreateDialog(savedInstanceState);


    // request a window without the title
    dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);

    //  closeDialog = (ImageView) dialog.findViewById(R.id.closeDialog);

    return dialog;

}



public void getAdapter() {

        gridAdapter = new HomeSubGridViewAdapter(getContext(), R.layout.gridview_custom_layout, liststring);
        gridView.setAdapter(gridAdapter);

}


}

这是从片段

调用对话框的方法
  fragmentManager = ((FragmentActivity) context).getSupportFragmentManager();
                    SubcategoryFragment postalFragment = SubcategoryFragment.newInstance(Integer.valueOf(item.getId()), item.getName());
                    postalFragment.show(fragmentManager, "SubcategoryFragment");

随时问你是否觉得有任何问题

答案 3 :(得分:0)

这是适用于我的代码:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    if (getArguments() == null) {
        throw new InvalidParameterException("The key types dialog needs the protocol id to be in the arguments");
    }
    if (mCallback == null) {
        throw new InvalidParameterException("The key types dialog needs an callback to be set");
    }

    mProtocolId = getArguments().getInt(ApplicationConstants.FragmentsConstants.PROTOCOL_ID);
    final List<KeyTypeEntity> allKeyTypes = BusinessFacade.getInstance(getActivity()).KeyTypeLogic.getAllKeyTypes();

    ArrayAdapter<KeyTypeEntity> keyTypeAdapter = new ArrayAdapter<KeyTypeEntity>(getActivity(), android.R.layout.simple_list_item_1, allKeyTypes);

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    builder.setTitle("").setAdapter(keyTypeAdapter, new OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            final KeyTypeEntity keyTypeEntity = allKeyTypes.get(which);
            AlertDialog.Builder number = new AlertDialog.Builder(getActivity());

            List<String> keyNumbers = new ArrayList<String>();

            for (int i = 0; i < 50; i++) {
                keyNumbers.add("" + (i + 1));
            }
            ArrayAdapter<String> kAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, keyNumbers);

            number.setTitle("").setAdapter(kAdapter, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    addNewKey(keyTypeEntity, which + 1);
                }
            });
            number.show();
        }
    }).setOnCancelListener(new OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            mCallback.onDialogClosed();
        }
    });

    mDialog = builder.create();
    return mDialog;
}

在第一个单击处理程序中,我只需创建一个新对话框并显示它。这将关闭第一个对话框,打开第二个对话框,当用户单击列表中的项目时,将调用第二个单击处理程序。

希望这会有所帮助,我也不会太迟:)

答案 4 :(得分:0)

您可以将FragmentManage传递给newInstance() DialogFragment方法,然后您可以使用它来显示新的dialogfragment 这是我的代码。

private static FragmentManager fragmentManager;

public static PlayListDialog newInstance(Context context,  FragmentManager fragmentManager1) {
    playListDialog = new PlayListDialog();
    mContext = context;
    fragmentManager = fragmentManager1;
    return playListDialog;
}

@Override
public void createNewPlaylist() {
    NewPlayListDialog newPlayListDialog = NewPlayListDialog.newInstance(mContext);
    newPlayListDialog.showDialog(fragmentManager.beginTransaction(),fragmentManager.findFragmentByTag("newdialog"));
}

答案 5 :(得分:0)

最近,我遇到了这个问题,上面没有一个选项适合我。我尝试使用以下方法:

DialogFragment fragment = new MyFragment(); //where MyFragment is my fragment I want to show
fragment.setCancelable(true);
fragment.show(getSupportFragmentManager(), "timePicker");

如果你在一个活动中使用它(即从活动类中调用一个对话框片段),这将 ONLY 工作。

我通过将我的活动实例向下转换为AppCompat活动并使用它来调用getSupportFragment()来修复此问题,如下所示:

DialogFragment timeFragment = new TimePicker();
timeFragment.setCancelable(true);
AppCompatActivity activity = (AppCompatActivity) getActivity();
timeFragment.show(activity.getSupportFragmentManager(), "timePicker");

我希望这有帮助..快乐编码!!

答案 6 :(得分:0)

您可以从另一个 DialogFragment中调用 DialogFragment

NewDialogFragment newDialogFragment= new NewDialogFragment();
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
newDialogFragment.show(transaction, "New_Dialog_Fragment");

答案 7 :(得分:0)

使用此:

getActivity().getSupportFragmentManager

代替

getChildFragmentManager().

希望这会有所帮助。

答案 8 :(得分:0)

如果您要使用kotlin版本,请使用以下命令:

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable();
    http.cors();
    http.authorizeRequests().anyRequest().fullyAuthenticated();
    http.httpBasic();
  }
}