Android: Can i show multiple Dialogs one over another? Is there something like Dialog Z-Level?

余生颓废 提交于 2019-12-07 02:51:09

问题


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.


回答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.

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();
        }
    }
}



回答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.




回答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)



来源:https://stackoverflow.com/questions/10282927/android-can-i-show-multiple-dialogs-one-over-another-is-there-something-like-d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!