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?
Related
Some people are getting this even tho I check if the activity is finishing or not. This app has a few million users and only 10 or so people are crashing. I am confused as to why.
private fun someFun(headerTitle: String, message: String) {
if (isFinishing) return
ComponentPopup.Builder(this)
.setTitleText(headerTitle)
.setMessageText(message)
.setPositiveButtonText("Some text")
.setNegativeButtonText("Some text")
.createPopup()
.show()
}
ComponentPopup is just a custom component that extends Dialog.
Crashlytics says I get this exception at .show(). I don't call this function on a background thread either. Only on onBackPressed().
What gives? Why do I get this exception even though I check isFinishing?
Tried to finish the activity manually. Still wasn't able to produce the problem.
I was getting the same exception. In my case the issue was I was passing the application context to the Builder instead of the Activity context. Hopefully, you are not doing so.
Just for testing, you may pass the context of your current Activity as YOUR_ACTIVITY.this instead of this.
This Issue happens when the user leave the activity before the diloag shown. So the dilaog didn't get the window of the specified Actvity to be show on.
You can generate the crash by calling the show of dialog and then instantly leave the activity.
The answer I expected at first was the show method. Unfortunately if show has a lot of work to do, there is a delay between method call and screen appearance. So how does one get notified when screen appears?
Using show() should be fine, the screen will be shown straight after so just put stuff you want done at the end where the delay should be negligible.
If the work that show() does is in a super class, then just override it, call super.show() and then do your stuff after, like so...
#Override
public void show () {
super.show();
doAnyOtherSlowStuffThatMightNeedDoing();
doTimeCriticalStuff();
}
I should probably add that a better solution would be to not do slow stuff in the show() method to start with, but that's a whole other debate.
I am new in android and I am learning from developer.android.com site. Then I came across to AlertDialog.dismiss() where in site it is written that
This method Dismiss dialog and remove it from the screen. This method can be
invoked safely from any thread. Note that you should not override this
method to do cleanup when the dialog is dismissed, instead implement
that in onStop().
But I did not understand the mean of this line-
Note that you should not override this method to do cleanup when the
dialog is dismissed, instead implement that in onStop()
what is the mean of above line?
`.
AlertDialog.dismiss() uses to dismiss the dialog if it's opened up as describe at developer site
Note that you should not override this method to do cleanup when the dialog is dismissed, instead implement that in onStop().
The above statement simply means that as we used to garbage collect object which is no more referenced in class and avail for garbage collect. They are simpling stating that the approach like avail for garbage collection also applies in here but there are eligible inside onStop() of Activity.
So better to use it as onStop() as it's the last call of Activity Life Cycle which can dismissed your alertdialog. If it incase is there on the screen without dismissal.
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.
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