Show DialogFragment from another DialogFragment
Asked Answered
M

9

20

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();
    }
}
Monophthong answered 14/5, 2013 at 9:57 Comment(2)
seems i cant show a DialogFragment from the onClick method !!!Monophthong
where do you instantiate the second fragment that you want to show?Allotropy
H
11

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.

Heartbreaker answered 20/4, 2016 at 16:44 Comment(0)
S
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();

Steppe answered 23/5, 2016 at 21:5 Comment(0)
J
2

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

Jarrod answered 31/10, 2016 at 12:37 Comment(0)
P
2

You can call a DialogFragment from Another DialogFragment.

NewDialogFragment newDialogFragment= new NewDialogFragment();
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
newDialogFragment.show(transaction, "New_Dialog_Fragment");
Porshaport answered 12/9, 2019 at 7:24 Comment(0)
C
1

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!!

Chirk answered 28/2, 2018 at 6:18 Comment(0)
A
0

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

Allotropy answered 31/5, 2013 at 8:53 Comment(1)
I don't really get it... why post ton of code from your project which has nothing to to with the question? It just messes up with beginners mindsCountersubject
V
0

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"));
}
Vashtee answered 7/11, 2017 at 3:43 Comment(0)
S
0

Use this:

getActivity().getSupportFragmentManager

instead of

getChildFragmentManager().

Hope this helps.

Scold answered 22/9, 2019 at 12:30 Comment(0)
S
0

If you want the kotlin version use this:

val newDialogFragment = NewDialogFragment()
val transaction: FragmentTransaction = 
requireActivity().supportFragmentManager.beginTransaction()
newDialogFragment.show(transaction, "New_Dialog_Fragment")
Sturgeon answered 29/3, 2020 at 5:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.