Skip to content Skip to sidebar Skip to footer

Android: Can I Show Multiple Dialogs One Over Another? Is There Something Like Dialog Z-level?

Is it possible to show multiple Dialogs one over another? Is there something like Dialog Z-Level? I am using DialogFragment where user chooses elements, when he comfirms his choice

Solution 1:

Indeed, it's possible to show multiple dialog Fragments one inside another one. The z-order depends on the order they are created.

In the code below there is an example of a FragmentActivity with the behavior that you require.

publicclassMyActivityextendsFragmentActivity {

    @OverridepublicvoidonCreate(Bundle savedInstanceState) {
        //...    
    }

    publicvoidonSave(View view) {
        Intentintent= getIntent();

        this.setResult(RESULT_OK, intent);
        finish();
    }

    publicvoidonCancel(View view) {
        finish();
    }

    publicvoidSelectWeekDay(View view) {
        DialogFragmentselectWeekDayFragment=newSelectWeekDayFragment();
        selectWeekDayFragment.show(getSupportFragmentManager(), "WeekDayDialog");
    }

    publicclassSelectWeekDayFragmentextendsDialogFragment {

        @Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            Viewview= inflater.inflate(R.layout.week_day_dialog, container, true);

            ButtonsaveButton= (Button) view.findViewById(R.id.button_save);
            saveButton.setOnClickListener(newView.OnClickListener() {

                @OverridepublicvoidonClick(View view) {
                    CheckBoxcheckboxMonday= (CheckBox) getDialog().findViewById(R.id.checkBox_monday);
                    if (!checkboxMonday.isChecked()) {
                        DialogFragmentsaveErrorFragment=newSaveErrorFragment();
                        saveErrorFragment.show(getSupportFragmentManager(), "SaveErrorFragment");
                    }
                    else {
                        SaveToDb(); //Perform actions to store on db or what you wish
                        dismiss();  
                    }
                }
            });

            ButtoncancelButton= (Button) view.findViewById(R.id.button_cancel);
            cancelButton.setOnClickListener(newView.OnClickListener() {

                @OverridepublicvoidonClick(View v) {
                    dismiss();
                }
            });

            return view;    
        }
    }

    publicclassSaveErrorFragmentextendsDialogFragment {

        @Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) {
            returnnewAlertDialog.Builder(getActivity())
            .setMessage("You must select Monday").setPositiveButton("Ok", null).create();
        }
    }
}

Solution 2:

My advice is to use a custom layout with a ViewFlipper inside your dialog so you can easily switch between a progress-bar or whatever different layouts you want to show. If you want to show multiple Dialogs my guess is that the z-order depends on the order they were created the latest beeing shown on top.

Solution 3:

You usually can, however, just be a little careful. Use the dialog's lifecycle to your advantage to avoid side-effects. For example: you can do a check on a function like onStop() to see if the child dialog is open, and if so, close it.

Ideally, cutting down on the amount of layers of dialogs you have is ideal, as long as it's sane (for example: doing it ends up being hundreds of lines of code more)

Post a Comment for "Android: Can I Show Multiple Dialogs One Over Another? Is There Something Like Dialog Z-level?"