android AlertDialog dismiss is not called - android

I've got some problem..
Well, I create AlertDialog using AlertDialog.Builder, than I using setContentView for setting OK and Cancel buttons and hang up some actions on the dismiss() method. BUT..when I touching anything else this buttons, dismiss() is not called.
So, my question is, Which methods are invokes when i touching and how can i call my methods in this case(touching anything else)?
Thns!

try to set onCancelListener() on the dialog.

Related

Android DialogFragment Viewmodel destroyed after dismiss problem

I have DialogFragment in which I show 2 buttons. One button is to submit API call (it executes function submitBonusDecision(). It goes like this:
private fun submitBonusDecision(decision: Boolean) {
BONUS_IDENTIFIER.argumentValue()?.let { it ->
bonusViewModel.submitBonusDecision(decision, it)
}
dismiss()
}
This code should call function bonusViewModel.submitBonusDecision() in my ViewModel and submit code. But here comes the problem. It looks to me that dismiss() is called before the API call is executed. I guess dismiss destroys DialogFragment and probably ViewModel together with it even before the API call is executed. Please tell me does this way of thinking makes sense to you.
Also if it does, can you give advice how to solve this problem in android? Whenever we have dialog or DialogFragment, we usually want to close it after the button click. How to solve this problem and succeed in doing the work even after dialog dismiss? Is it possible to do this in ViewModel?

How to cancel or dismiss a custom dialog in its onCreate method?

I have created a custom dialog called MyCustomDialog which extends Dialog. I create and show my custom dialog as follows:
new MyCustomDialog(myContext).show();
I override the Dialog.onCreate(Bundle savedInstanceState) method to do my initialisation. I also check in this method whether a certain condition holds and, if not, I would like to dismiss/cancel my dialog. I have tried calling the cancel() and dismiss() methods in my dialog's onCreate(Bundle savedInstanceState) and onStart() methods but it has no effect.
Anyone know how to cancel or dismiss a dialog (from within the dialog) before it shows?
You should place the logic to determine if the dialog is to be shown outside of the onCreate() method. it does not belong there.
Alternatively, rename your show() method showIfRequired() (or something), and add the conditional show logic there.
I know this doesn't technically answer your question, but what you are trying to do is not the correct design. That's a good thing, as doing in the right way is actually simpler.
Also, as a side note, you should using DialogFragment in favor of Dialog. it's available in the v4 support library.
This is for API levels 10 and below:
First you should override onCreateDialog(int id, Bundle args) in the Activity class, is that what you're doing? Dialogs are always created and displayed as part of the Activity. Second, I don't think you can cancel/dismiss a dialog in onCreateDialog because it hasn't actually been created when onCreateDialog is called. That is, you can't cancel/dismiss something that hasn't been created. What you can try is to override onPrepareDialog() instead and do your check to cancel/dismiss the dialog there. At that point the dialog should actually have been created (just not displayed), so you would be able to prevent it from getting displayed if you call cancel/dismiss there.
onPrepareDialog() is the proper place to do any sort of checks and decision making on the dialog that is about to be displayed. This is for APIs prior to Honeycomb.
This is for APIs 11 and later:
If you are using a later API, you should extend DialogFragment instead. In this case I think you can handle the decision making in onCreateView() method of DialogFragment which is similar to onPrepareDialog().
I hope you've read through this:
http://developer.android.com/guide/topics/ui/dialogs.html
or this, depending on your API:
http://developer.android.com/reference/android/app/DialogFragment.html
Overall, perhaps a cleaner solution is to disable the button or mechanism that causes the dialog to show up in the first place? That is, write you code such that Dialog.show() is called only when it really needs to be called. I'd have to know more details about what exactly you're trying to do. For example, say you call Dialog.show() from the onClickListener of a button. you don't really want the user to press a button, expect a dialog, but have it not show up due to some reason the user doesn't understand. A better solution would be to disable the button all together so that it's obvious to the user that this function isn't available due to something else in the application.

Android dialog rotation issue.

I have a Dialog which depends on information from the current view. This works fine until the unit is rotated at which point the dialog tries to display before the view is built. The dialog is non essential and I'm happy to scrap it on rotation rather than try to replicate the information and hold it for the dialog to be reconstructed.
I've tried calling removeDialog(PHOTO_CREDIT_DIALOG) from the activities onPause method but it doesn't seem to do anything. I've also held a reference to the dialog and tried calling dismiss() in onPause. Also to no avail. The same call when run from a button on the dialog does infact remove it.
thanks,
m
Does onPause get called ? What if you try to remove the Dialog in onConfigurationChanged instead?
http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange

When should i dismiss dialogs?

I've done some reading about dialogs in Android and I have an open issue:
When I'm using the Activity's methods that handle the dialogs (such as: onCreateDialog(...)), should I or shouldn't I dismiss the dialog in the onPause()?
Or maybe I should dismiss it only if I retained it - made an Activity member variable that has a reference to this dialog?
I've found this answer: https://stackoverflow.com/a/2851833/501560 saying that I need to explicitly call the dismiss() method, but I've read some other resources saying that the Activity should handle it by itself...
Thanks.
Dismissing a Dialog
When you're ready to close your dialog, you can dismiss it by calling dismiss() on the Dialog object. If necessary, you can also call dismissDialog(int) from the Activity, which effectively calls dismiss() on the Dialog for you.
If you are using onCreateDialog(int) to manage the state of your dialogs (as discussed in the previous section), then every time your dialog is dismissed, the state of the Dialog object is retained by the Activity. If you decide that you will no longer need this object or it's important that the state is cleared, then you should call removeDialog(int). This will remove any internal references to the object and if the dialog is showing, it will dismiss it.
Using dismiss listeners
If you'd like your application to perform some procedures the moment that a dialog is dismissed, then you should attach an on-dismiss listener to your Dialog.
First define the DialogInterface.OnDismissListener interface. This interface has just one method, onDismiss(DialogInterface), which will be called when the dialog is dismissed. Then simply pass your OnDismissListener implementation to
setOnDismissListener().
However, note that dialogs can also be "cancelled." This is a special case that indicates the dialog was explicitly cancelled by the user. This will occur if the user presses the "back" button to close the dialog, or if the dialog explicitly calls cancel() (perhaps from a "Cancel" button in the dialog). When a dialog is cancelled, the OnDismissListener will still be notified, but if you'd like to be informed that the dialog was explicitly cancelled (and not dismissed normally), then you should register an
DialogInterface.OnCancelListener with setOnCancelListener().
You never have to dismiss the dialog if it's managed by the Activity.
The Activity will dismiss the dialog when it's destroyed. If the Activity is pause, Dialog doesn't have to be dismissed.
I thought dialogs are supposed to be dismissed in onStop() or onPause() in the Activity lifecycle.
https://developer.android.com/reference/android/app/Dialog.html#dismiss()

How to preload a dialog

I have a custom dialog and despite putting in all the code to inflate the view in its constructor, it seems like it takes much longer when the dialog is first launched compared to subsequent launches, as if only when i call dialog.show(); it actually creates it. How could I do what I intended, to properly preload the dialog to prevent this first run delay?
You can Extend your class with AsyncTask<> and perform all work in doInBackground().in onPreExecute() Show Dialog and in onPostExecute() remove the dialog from UI.

Categories

Resources