Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show DialogFragment from another DialogFragment

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();
    }
}
like image 382
user4o01 Avatar asked May 14 '13 09:05

user4o01


People also ask

How do you show DialogFragment?

Showing the DialogFragment It is not necessary to manually create a FragmentTransaction to display your DialogFragment . Instead, use the show() method to display your dialog. You can pass a reference to a FragmentManager and a String to use as a FragmentTransaction tag.

Can a dialog open another dialog?

A Dialog can be nested within another Dialog. The below sample contains parent and child Dialog (inner Dialog).

Is DialogFragment deprecated?

This method is deprecated. androidx.

What is the difference between dialog & DialogFragment?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.


1 Answers

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.

like image 177
Yoann Hercouet Avatar answered Sep 28 '22 06:09

Yoann Hercouet