Show DialogFragment from another DialogFragment

前端 未结 9 734
春和景丽
春和景丽 2021-02-05 10:33

I have a DialogFragment that displays a list of options to the user, one of these options is \"Delete\" option, when the user presses the delete option I want to sh

相关标签:
9条回答
  • 2021-02-05 11:13

    Use this:

    getActivity().getSupportFragmentManager

    instead of

    getChildFragmentManager().

    Hope this helps.

    0 讨论(0)
  • 2021-02-05 11:15

    This is the code that works for me:

    @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;
    }
    

    In the first click handler I just create a new dialog and show it. This will close the first dialog, open the second one, and when the user clicks on an item in the list, the second click handler is called.

    Hope this helps, and I am not too late :)

    0 讨论(0)
  • 2021-02-05 11:17

    You can pass FragmentManage to newInstance() method of First DialogFragment then you can use it to show new dialogfragment this is my code.

    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"));
    }
    
    0 讨论(0)
提交回复
热议问题