![]() ![]() environment that skips onStart then the dialog will still be functioningįinal AlertDialog d = (AlertDialog)getDialog() īutton positiveButton = (Button) d.getButton(Dialog.BUTTON_POSITIVE) Doing it in onResume() makes sure that even if there is a config change the underlying dialog, so we have to do it there or onStart() is where dialog.show() is actually called on Make sure you have an obvious way to close the dialog especially if you set cancellable to false.ĭialogFragment - override onResume() Dialog onCreateDialog(Bundle savedInstanceState)ĪlertDialog.Builder builder = new AlertDialog.Builder(getActivity()) Do stuff, possibly set wantToCloseDialog to true then. Overriding the handler immediately after show is probably a better approach than OnShowListener as described belowĭialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new void onClick(View v) pass a handler the button doesn't get instantiatedįinal AlertDialog dialog = builder.create() However, we still need this because on older versions of Android unless we Do nothing here because we override this button later to change the close behaviour. New void onClick(DialogInterface dialog, int which) Note: A description of how the dialog closing works under the hood for the base android classes and why the following approaches are chosen follows after the examples, for those who want more detailsĪlertDialog.Builder - Change default button handler immediately after show() AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()) ītMessage("Test for preventing dialog close") All the examples show how to prevent the positive button from closing the dialog. There are solutions for AlertDialogs using AlertDialog.Builder, DialogFragment, and DialogPreference.īelow are the code examples showing how to override the default common button handler and prevent the dialog from closing for these different forms of dialogs. Here are some solutions for all types of dialogs including a solution for AlertDialog.Builder that will work on all API levels (works below API 8, which the other answer here does not).
0 Comments
Leave a Reply. |