Android: Can i show multiple Dialogs one over anot

2020-03-19 07:17发布

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, it is saved to database and sent on server. if the save action fails I would like to inform user with ... another dialog is it possible? And will it not clear off my first dialog? Thanks in advance.

3条回答
聊天终结者
2楼-- · 2020-03-19 07:30

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.

查看更多
▲ chillily
3楼-- · 2020-03-19 07:35

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)

查看更多
爷的心禁止访问
4楼-- · 2020-03-19 07:56

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.

public class MyActivity extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        //...    
    }

    public void onSave(View view) {
        Intent intent = getIntent();

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

    public void onCancel(View view) {
        finish();
    }

    public void SelectWeekDay(View view) {
        DialogFragment selectWeekDayFragment = new SelectWeekDayFragment();
        selectWeekDayFragment.show(getSupportFragmentManager(), "WeekDayDialog");
    }

    public class SelectWeekDayFragment extends DialogFragment {

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

            Button saveButton = (Button) view.findViewById(R.id.button_save);
            saveButton.setOnClickListener(new View.OnClickListener() {

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

            Button cancelButton = (Button) view.findViewById(R.id.button_cancel);
            cancelButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });

            return view;    
        }
    }

    public class SaveErrorFragment extends DialogFragment {

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            return new AlertDialog.Builder(getActivity())
            .setMessage("You must select Monday").setPositiveButton("Ok", null).create();
        }
    }
}
查看更多
登录 后发表回答