How to make alertDialog not lose focus until dismiss() is called - android

My alertDialog closes when I minimize my app. I don't want it to until alertBuilder.dismiss() is called. Also my alertBuilder depends on some user inputs so its not possible to open it again on activity resume. So how not to lose focus of it?
EDIT: My alertBuilder has some links in it and when user presses on one of the links,it is redirected to another app and then the alertBuilder closes.I want this not to happen.Please help me.

You can setCancelable(false) on your alert dialog.
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Your Title")
.setMessage("Your Message")
.setCancelable(false)
.setPositiveButton("OK",null)//optional
.show();
This way alert dialog does not close on activity minimize or back press.
Clicking positive or negative button finally dismisses alert dialog so these buttons might be provisions to dismiss the alert dialog.
You can supply null as onClickListener parameter, if don't want to execute any code on button click. This also dismisses alert dialog.

Related

Hide dialog in kotlin on certain conditions

Is it possible to achieve this in Kotlin Android studio.
I have two alert dialog
Dialog one shows on one event to notify users that the event is loading.
Dialog 2 shows when the first event finish loading.
The current results, if dialog one shows, within 2 seconds the activity finish loading and then shows dialog 2.
How can I hide dialog one, on condition of when dialog 2 shows?
This is my alert dialog code
AlertDialog.Builder(requireActivity())
.setTitle("Status upload")
.setMessage("Your image was successfully uploaded!") // Specifying a listener allows you to take an action before dismissing the dialog.
// The dialog is automatically dismissed when a dialog button is clicked.
.setPositiveButton(android.R.string.yes) { dialog, which ->
// Continue with delete operation
} // A null listener allows the button to dismiss the dialog and take no further action.
.setIcon(R.drawable.ic_check_success)
.show()
you can do this where the event ends
dialog1.dismiss()

Android Alert Dialog still attached to Context after click outside

Hi I have a strange Problem.
I have an Activity with a Fragment with a RecyclerView. When a user clicks on an item I show a simple alert dialog where the user can make some specifications. When the user has selected something within the dialog, I close the dialog and finish the activity to go back to the parent activity.
This works as expected but if the user selects nothing and closes the dialog with a click outside and goes back to the parent activity, leakcanary shows me that the dialog is still attached to the activity context. So my question is how to avoid that?
I have already tried to set an onCancel or an onDismissListener to dismiss the dialog myself but that is not working.
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setCancelable(true);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.setCancelable(true);
alertDialog.setCanceledOnTouchOutside(true);
This might work.
I solved it by adding a destroy where method where I dismiss the dialog.

Dialog: confirmation when dismissed

Topic: I want to be able to cancel a dismiss call on a dialog.
I am entering information thorugh a dialog. When data inside the dialog has changed and the user dismisses the dialog without saving (by pressing back or clicking outside of the dialog), I want to be able to prevent that dismis by showing a confirmation dialog, that asks the user if he really wants do dismiss the dialog.
An analagy for what I am looking for is in VBA, where the cancel-variable of a beforeSave-listener can be set to "true", so that the file is not saved, even though save is pressed.
I could not find a solution that I can place inside the dismissListener of the dialog.
Thanks a lot in advance and best regards!
You can maintain the state of dialog open/ close in a boolean and handle it. When you open the dialog you make it true and when user clicks back or outside the dialog check that boolean and show the alert pop-up and when dialog closes (dismisses) make boolean to false.
user these methods on your dialog view to prevent cancel dialog
ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.setCancelable(false);
progressDialog.show();

Clicking outside "never show again" popup dialog

I used the answer from Cengiz Can from here
How to make a "do not ask me again" dialog pop-up box? Android
to set up a "never show again" dialog popup. It works but when the user clicks outside of the popup dialog, it closes.
How can I block the user from clicking outside in order that my popup does not get closed.
The dialog just should get closed when the user clicks "cancel" or "Ok" and not by clicking outside the box anywhere in the layout.
Is there a method to avoid this?
Thank you
Add set builder.setCancelable(false); in your dialog builder. it will not close user click on screen apart from button.
Add this line builder.setCancelable(false);
Example
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Test");
builder.setPositiveButton("ok", null);
builder.setCancelable(false);
builder.show();
If you want to avoid closing the dialog with the back key :
setCancelable(false);
If you want to avoid closing the dialog touching outside :
setCanceledOnTouchOutside(false);

How can i prevent an android dialog from dismiss when user press beside the dialog?

I have an Dialog (not an AlertDialog) which should only be dismissed when user press a button.
But in my case, the dialog dismiss also when user press beside the dialog.
How can i avoid that.
Thnx for help!
Use
dialog.setCanceledOnTouchOutside(false);
where dialog is your Dialog variable
Dialog docs
setCanceledOnTouchOutside(boolean cancel) or
setCancelable(boolean flag)
These are the methods that you can use to prevent Dialog from being dismissed
Set the cancelable state to false using this: '.setCalcelable(false)'
So your dialog code would look something like this...
Dialog mDialog = new Dialog(this);
mDialog.setCancelable(false);

Categories

Resources