Show DialogFragment from another DialogFragment

為{幸葍}努か 提交于 2019-12-04 16:50:14

问题


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 show another DialogFragment as a confirmation, unfortunately, the confirmation dialog doesn't show.

here is my code

First Fragment code

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();
    }
}

回答1:


I got the exact same problem, this situation does not happen when you try to open a DialogFragment from a Fragment.

The only solution I found was to modify the following call:

fragment.show(ft, fragmentTag);

To:

fragment.show(getFragmentManager(), fragmentTag);

The problem with this solution is that we cannot work on the FragmentTransition.

I don't understand why the behavior is different than with the fragments.




回答2:


I came across the same problem of not being able to show another DialogFragment from within the positive and negative click listeners of the first DialogFragment. My solution was to immediately pop the first fragment, which allows the second DialogFragment to attach and display successfully.

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




回答3:


Please check this following code. Hope this will help many of you!

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);

}


}

This is the method for calling dialog from fragment

  fragmentManager = ((FragmentActivity) context).getSupportFragmentManager();
                    SubcategoryFragment postalFragment = SubcategoryFragment.newInstance(Integer.valueOf(item.getId()), item.getName());
                    postalFragment.show(fragmentManager, "SubcategoryFragment");

Feel Free to ask if you feel any problem is that




回答4:


Very recently, I had this problem and none of the options above worked for me. I tried using the method below:

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

This will ONLY work if you're using this in an activity (i.e to call a dialog fragment from an activity class).

I however fixed this by downcasting my activity instance to an AppCompat activity and using it to call getSupportFragment() as shown below:

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

I hope this helps.. Merry coding!!




回答5:


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 :)




回答6:


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"));
}



回答7:


You can call a DialogFragment from Another DialogFragment.

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



回答8:


Use this:

getActivity().getSupportFragmentManager

instead of

getChildFragmentManager().

Hope this helps.



来源:https://stackoverflow.com/questions/16540186/show-dialogfragment-from-another-dialogfragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!