Android: Can I show multiple Dialogs one above the other? Is there something like Dialog Z-Level?

Is it possible to show several dialogs one after another? Is there something like Dialog Z-Level? I use DialogFragment, where the user selects the elements, when he confirms his choice, it is saved in the database and sent to the server. if the save action fails, I would like to tell the user ... another dialog? And won't that clear my first dialogue? Thanks in advance.

+6
source share
3 answers

In fact, it is possible to show several fragments of the dialogue one inside the other. Z-order depends on the order of their creation.

The code below has an example of a FragmentActivity with the required behavior.

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

My advice is to use a custom layout with ViewFlipper inside your dialog so that you can easily switch between the progress bar or any other layouts you want to show. If you want to show several Dialogs, then I assume that the z-order depends on which order was created, the last of which is shown above.

0
source

However, you can usually be a little careful. Take advantage of the dialogue life cycle to avoid side effects. For example: you can test a function such as onStop () to see if the child dialog is open, and if so, close it.

Ideally, reducing the number of dialog layers you have is ideal if it is normal (for example: doing this ends up with hundreds of lines of code more)

0
source

Source: https://habr.com/ru/post/913947/


All Articles