Show DialogFragment from another DialogFragment

user4o01 picture user4o01 · May 14, 2013 · Viewed 14.8k times · Source

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

Answer

Yoann Hercouet picture Yoann Hercouet · Apr 20, 2016

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.