Alert dialog not closing - "Cancelling event due to no window focus" - android

I have an Alert Dialog of type TYPE_SYSTEM_ALERT with 2 buttons Ok & Cancel. The OK button opens a new activity where the Cancel button just dismisses the dialog.
The alert it's being displayed 2 times one on top on the other - and if the Cancel button is pressed for the visible dialog - it's being dismissed and the second one it's being displayed and everything works as it should.
My problem is when selecting OK from the visible dialog - the required activity is being opened, the current dialog is being dismissed but a weird thing is happening - the activity is displayed beneath the first dialog and when trying to select one of the buttons the message error "Dropping event due to no window focus: MotionEvent" is displayed - and the dialog can not be accessed anymore.
I've tried with an event to request focus for the remaining Alert - something like:
this.mAlertDialog.getWindow().getDecorView().setFocusable(true);
this.mAlertDialog.getWindow().getDecorView().setFocusableInTouchMode(true);
this.mAlertDialog.getWindow().getDecorView().requestFocus();
or
this.mAlertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).requestFocus();
but no change.

Try one of these
this.mAlertDialog.getWindow().getDecorView().setCancelable(true);
OR
this.mAlertDialog.setCancelable(true);
OR
...onclick(View v) {
this.mAlertDialog.cancel();
}
OR
...onclick(View v) {
this.mAlertDialog.dimiss();
}

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.

Android - How to show warning dialog below confirm dialog?

I have confirm dialog(A) and warning dialog(B).
Dialog A already show on top.
An event come, dialog B will show on.
I want dialog B will show below dialog A.
Is it possible?

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.

Alert dialog is opening two times results crash of application

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.

Categories

Resources