I'm trying to have my alert dialog not only close itself when cancelled but also go back to the activity before the one it was called in. Is there a cancel function I can override or is it better to setCancelable(false) and use a KeyListener on the back button?
When using AlertDialog.Builder to create your AlertDialog you can set a OnCancelListener (OnCancelListener docs).
Then in the listener you can call finish() to destroy the activity.
Related
I have a dialog fragment that has a close button on it.
This dialog is called from two different activities. One uses it as a simple fragment, and one as a dialog fragment.
In its onClick method, I call requireActivity.onBackPress() so it will close the dialog.
I don't want to call dismiss, because I have a use case in which I want the calling activity to handle it (show a confirmation dialog).
In the activity that shows it as a dialog fragment, calling the onBackPress closes not only the dialog but also finishes the activity :scary_face
Pressing the actual back button just dismisses it though.
Any thoughts on what am I missing here?
Code from the dialog fragment:
viewBinding.closeBtn.setOnClickListener {
requireActivity().onBackPressed()
}
Code that launches the dialog fragment
SharePatientDialogFragment.newInstance(shareData)
.show(context.supportFragmentManager, "share")
You're calling the onBackPressed() of the activity and not from the DialogFragment.
Hence, your activity is closing, and therefore the dialog is also closing.
To fix it, you have to invoke the onBackPressed() of the dialog fragment to ensure that the dialog fragment is dismissed.
Fragment does not possess an onBackPressed() method but the Dialog class does, as so you need to check if it's a dialog and then invoke the method if available.
I have an alert dialog in my activity. When the user presses the home button when the dialog is dismissed, i would like to have it re-open when the activity resumes.
Right now, i am dismissing the alert onPause() event of the activity.
How can i do this?
probably you could have a variable that indicates that the alert dialog is showing.
bool isAlertShown=false;
set it to true when the dialog is shown.
set it to false when the dialog is dismissed.
onResume event of your activity, check if the variable is true, if yes, you should be showing the dialog.
Show dialogs with DialogFragment, it handles state automatically.
if you want to show every time when your activity resumes show it in your activity's onResume()
if you want to show dialog in case dialog was showing before user pressed home declare a variable such as "isDialogShown" and set it true or false. Do not forget to save your flag variable onSavedInstanceState()
I advice to use Dialog Fragments (http://android-developers.blogspot.com.tr/2012/05/using-dialogfragments.html) for these kind of operations.
I am working with Android, and I open a Dialog using myDialog.show(); from the Activity, this Dialog has a Button and when I click that button the Dialog closes without problem using this.hide();.
this is the part of code where I have the question :
myDialog.show();
Toast.makeText(this, "the dialog is closed", Toast.LENGTH_SHORT).show();
the Toast is displayed during myDialog is open, and I thought that when myDialog is open, this hold my Activity and the Toast can not be displayed, but it is not the case.
So what I want is that myDialog once opened hold the Activity and when it is closed the Activity continue to the next instruction which is the Toast
just Override onDismiss in your dialog and put your toast in there. onDismiss gets called when the dialog is closing.
of in your activity implement OnDismissListener and set the listener in your dialog
I think that a similar question is asked here, and the solution that proposed is to create your myDialog.setCancelable(false);
I find this here :
If you want to literally not have the function that brings up the dialog return until the dialog is closed, you're in for some trouble. That's not the way the Android UI works.....
Friends i have an application with an Activitiy which brings some data from Content Providers and Display it in the textViews and edittext onto the Screen. but before that it prompts me for the username and password in a dialog. i have done all the getting content Providers Stuff in the positiveButton onClick Listener of the Alert Dialog.
It works fine but problem is that if i dont enter username and just press bakc key button it closses down the Dialog Box and the Back Screen is showed without loading the content providers.
Note: I have put my code of alert dialog in the onCreate Function of that activity.
So can u guide me how should i do it that when i press back key on dialog box it also should not display my activity.
Please Help!
your dialog name here.setCancelable(false);
this is working
I would honestly just do:
mDialog.setCancelable(false); //assuming the field mDialog is your Dialog
Then make sure you have both an Okay and Cancel (Positive and Negative) buttons on your Dialog. This way, the back press will do nothing, and you can use the Cancel button to finish your activity as well if desired.
The Dialog interface provides a setCancelable() method which enables precisely this. You call that with a false value and the user won't be able to press the back button to go back to your activity.
You can use method of each activity is : onBackPressed()
http://developer.android.com/reference/android/app/Activity.html
-This is the method which you can override sub method to execute when user enter back key.
I want dismissDialog(ID) to be called whenever dialog is gone (disapears, get closed , ...), so it may happen when user press BACK button or any other scenario that may close the dialog.
which one is better approach? to call onCancelListener on dialog? or call OnKeyListener and assign if (keyCode == KeyEvent.KEYCODE_BACK)
// do smth
thanks.
Use onBackPressed(), to do cleanup or whatever you want to do in dismissdialog()
if you do not ant to allow to dialog disappear,when back button pressed.it can be done by setting dialog's property as below:
dialog.setCancelable(false);//here dialog is object of Dialog class which you want to show