Android Multiple Nested DialogFragment

I have a script in which I show DialogFragment(using getSupportFragmentManager()) that shows a list of values ​​and an Add button. If the user clicks the add button, I show another one DialogFragment(using getChildFragmentManager()which consists of EditTextand Button. When the user clicks a new one Button, if the value passes the check, I add it to the list. If it does not pass the test, I show another one DialogFragmentthat displays an error message , and the β€œOK.” button. I would like for the user to see DialogFragmentusing EditTextand Button, so that they can view the bad value and possibly change it. However, when in the error messageDialogFragmentthe OK button is pressed, the dialog is rejected and only the original is displayed DialogFragment(the one that has a list of values).

How would I keep the second DialogFragmentstill displayed after the third is disabled?

EDIT: The following are some snippets of code:

//clicking this button shows the first ("outer") fragment
findViewById(R.id.voicemail_notifications_email_addresses).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            AddressListDialog.newInstance( 
                   line, AddressListDialog.VOICEMAIL_NOTIFICATIONS_EMAIL)
                  .show(((FragmentActivity)getContext()).getSupportFragmentManager(), "dialog");
        }

});

public static class AddressListDialog extends DialogFragment {
    public AddressListDialog() {}

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final View v = LayoutInflater.from(getActivity()).inflate(R.layout.vm_address_list_dialog, null);
        ListView lv = (ListView) v.findViewById(R.id.voicemail_notifications_address_list);
        final AddressAdapter adapter = new AddressAdapter();
        lv.setAdapter(adapter);

        v.findViewById(R.id.voicemail_add_address_button).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                //first child fragment
                AddAddressDialog.newInstance(getType()).setAdapter(adapter)
                                .show(getChildFragmentManager(), "dialog");
            }

        });

        Dialog d = new AlertDialog.Builder(getActivity())
                .setTitle(titleRes)
                .setView(v)
                .setPositiveButton(R.string.global_dialog_pos, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            dialog.dismiss();
                        }
                    }
                )
                .create();
        return d;
    }

    private class AddressAdapter extends BaseAdapter {
        .
        .
        .
        public boolean add(LineVoicemailInfo.VmContact contact) {
            if(addresses.size() >= 3) {
                return false;
            }
            else if(!contact.isValid(isEmailType())) {
                return true;
            }
            else {
                addresses.add(contact);
                notifyDataSetChanged();
                return true;
            }
        }

public static class AddAddressDialog extends DialogFragment {
    private AddressAdapter adapter;

    public AddAddressDialog() {}

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        DialogInterface.OnClickListener onPosClick = null;

        final View v;
        v = LayoutInflater.from(getActivity()).inflate(R.layout.voicemail_add_email_address, null);
        onPosClick = new DialogInterface.OnClickListener() {                        
            @Override
            public void onClick(DialogInterface dialog, int which) {
                LineVoicemailInfo.VmContact contact = new LineVoicemailInfo.VmContact(((EditText)v.findViewById(R.id.voicemail_add_email_address)).getText().toString());
                if(!adapter.add(contact)) {
                    //second child fragment
                    BadAddressDialog.newInstance("Not a valid address")
                                    .show(getChildFragmentManager(), "dialog");
                }
            }
        };


        Dialog d = new AlertDialog.Builder(getActivity())
                .setTitle("Add Address")
                .setView(v)
                .setCancelable(false)
                .setPositiveButton(R.string.voicemail_addresses_add, onPosClick)
                .setNegativeButton(R.string.global_dialog_neg, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                           dialog.dismiss();
                        }
                })
                .create();
        setCancelable(false);
        return d;
    }
}

public static class BadAddressDialog extends DialogFragment {           
        public BadAddressDialog() {}

        public static BadAddressDialog newInstance(String message) {
            BadAddressDialog frag = new BadAddressDialog();
            Bundle args = new Bundle();
            args.putString("message", message);
            frag.setArguments(args);
            return frag;
        }

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {   
            String message = getArguments().getString("message");

            Dialog d = new AlertDialog.Builder(getActivity())
                    .setTitle("Error adding address")
                    .setMessage(message)
                    .setCancelable(false)
                    .setPositiveButton(R.string.global_dialog_pos, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            dialog.dismiss();
                        }
                    })
                    .create();
            setCancelable(false);
            return d;
        }
    }
+4
source share
1 answer

I assume the problem is that you are using a standard method showto display dialogs. According to Android sources, this method does not add the fragment to the still frames, so the topmost dialog is rejected, and the middle one is also rejected.

trasaction , show backstack ( addToBackStack). .

+5

All Articles