Alert dialog is opening two times results crash of application - android

In my android app i am trying to click a button which does some calculation result a alert dialog. asking user yes or not.
Clicking yes perform the action and No dismiss the alert dialog.
Problem is when i am clicking the button very fast it opens two alert box some times , pressing yes in first alert dialog does its action but pressing yes on second result in crash.
What will be the best approach to solve this problem . Actually this is simple subjective logic that's why i am not adding code here .

Simply add:
if(dialog != null && !dialog.isShowing()) {
dialog.show();
}
To your onClick() method. This checks to see if the dialog is showing or not, and only shows it if it isn't already visible.
You'll have to replace dialog with whatever your instance is called.

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

How to show the same DialogFragment twice or more

My application has a button that shows a custom DialogFragment when the user presses a button. The DialogFragment is shown like this:
if(searchDialog == null)
{
searchDialog = new SearchElementDialog();
searchDialog.setOnItemClickListener(searchElementItemClickListener);
}
searchDialog.show (getFragmentManager(), "SearchElement");
When the user is finished dismiss is called in the usual way and the dialog is removed. Now, when the user presses the same button again I want to show the same dialog, in the same visible state as when the user left it, calling the code in the first block above does display the dialog and its visual state is restored, but:
The screen is not dimmed, and pressing outside of the dialog does not dismiss it and neither are the controls behind it responding.
The dialog does not move when the softkeyboard comes into view.
On the first showing everything is fine. Is it not possible to show the same instance of a dialog again?
What if you hide and show the dialog instead of the whole fragment.
When you want to dismiss call:
searchDialog.getDialog().dismiss();
and when you want to show it:
searchDialog.getDialog().show();
Simas was partly right. getDialog() doesn't return null when the Dialog is shown but it does when there is no Dialog displayed. I stored it as a field in my class
mDialog = getDialog()
and subsequently called
mDialog.show()
which worked every time.

How to disable BACKKEY when an alertbox is called

I am developing an android app in which alert box is called on click of a button.
I want that when alert box is displayed it should not disappear without answering to it. Not even by pressing BACK KEY . Only when button on alert box is pressed it should act only then otherwise not.
Thank you please reply soon Urgent.
myAlertBox.setCancelable(false);
Hi write your code inside this method
public AlertDialog.Builder setOnCancelListener (DialogInterface.OnCancelListener onCancelListener)
or
setCancelable(boolean)

Dialog does not open when called shortly after being dismissed

In my app, the user logs in with a custom made login dialog. The user can confirm and exit the dialog in two ways:
Press the Enter/Done button in the password box.
Press the OK button.
When the user has confirmed, the provided credentials are verified. If the credentials were incorrect, the dialog will reappear. This does only work if the user presses the Enter/Done button in the password box and not if the OK button is pressed. I use the same code for both the TextView.OnEditorActionListener and the DialogInterface.OnClickListener. I've tried debugging the code and I've discovered that in both cases, the boolean android.app.Activity.showDialog(int id, Bundle args) return true, which tells if the dialog was displayed or not.
I believe your best bet is to just create a new dialog. Should'nt be to hard.
UPDATE:
Also you could set it to
setVisibility(View.GONE);
And then when you want it to be show again
View.VISIBLE
Why don't you just hide() it? Only dismiss() it when you are really done with the Dialog

What is the best approach to make the dialog boxes persistent on the screens when application comes to foreground?

I want to know the best approach to handle the dialog boxes to be appeared when the user application comes to foreground.
My scenario would be like if am displaying some message in the progress dialog or normal alert dialog and i press the home button to take the application to background. And when i come back to the screen the progress dialog or alert dialog should be persisitent on the screen.
I tried putting this block in onStart() but it shows me NULL pointer exception.
onStart(){
if alertdialog.isShowing() alertdialog.show();
}
if(alertdialog == null){
// create your dialog here and then show it
} else if (!alertdialog.isShowing()) {
alertdialog.show();
}

Categories

Resources