Avoid AlertDialog to be dismissed when back button is pressed - android

I have an AlertDialog that shouldn't close when a certain condition, (a button of it isn't enabled) happens if the back button of the device is pressed.
With the following code, I've managed to partially achieve the desired behavior.
dialog1.setOnCancelListener(new DialogInterface.OnCancelListener()
{
#Override
public void onCancel(DialogInterface dialog)
{
Button button3 = ((AlertDialog)
dialog1).getButton(AlertDialog.BUTTON_NEUTRAL);
if (!button3.isEnabled())
{
dialog1.show();
}
else
{
dialog1.dismiss();
}
}
});
But this code presents 2 problems:
1) There's a small time where dialog1 stops showing to show again, this looks a bit bad.
2) Much more important, one needed button that displays when that button is disabled stops showing, this button doesn't initially show with the dialog, under some circumstance which also triggers that the shown button gets disabled it gets to show. For some reason, it looks like the dialog isn't refreshed to its last state and keeps only the elements that originally had.
Is there anyway so that if the back button is pressed when showing the dialog under the mentioned condition absolutely nothing happens or at the very least to keep the exact same elements it had when dismissed and is later shown again?

Use setCancelable();
Sets whether the dialog is cancelable or not. Default is true.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
this will not let you click outside the dialog to dismiss it, or simply go back closing it
dialog1.setCancelable(false);

Override the onBackPressed() and add check for button disabled:
#Override
public void onBackPressed() {
if (button3.isEnabled()) {
//do something
} else {
super.onBackPressed();
}
}

Related

How to dismiss the parent custom dialog when clicking Ok on the second dialog

I am showing two dialogs each should be displayed with different network call on the same activity (Login Activity). In which if I click on "Resend Email" text view in the first dialog then I am having another network call that shows me another dialog. When I click "OK" on the second dialog, it is dismissed. But the first one is still shown. So how to dismiss both when I click "Ok" on the second.
create a local Dialog variable dialogOne and when you click on ok of the dialog two dismiss both
btn_ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
isShown = true;
dialog.dismiss();
dialogOne.dismiss();
}
});
Dismiss both dialog when button clicked and before dismiss must check dialog is visible or not to avoid nullpointer exception.
btn_ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(dialog.isShowing())
dialog.dismiss();
if(firstdialog.isShowing())
firstdialog.dismiss();
}
});

How to enable back button wen setCancelable is set to false in Progress Dialog

I have a progress dialog, I have set progressDialog.setCancelable(false);,but i want to give user the option to use back button and go to previous activity.
Is it possible to enable back button when setCancableis set to false
Just finish your activity when the user presses the back button. Then they should return to the previous activity
#Override
public void onBackPressed() {
finish();
}

Dialog doesn't trigger the dismiss listener on a button press

I would like to display an ok cancel dialog to the user and I would like to know if use pressed ok, cancel, or if he chose to just dismiss the dialog by clicking elsewhere on the screen or pressing back button.
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
final EditText input = new EditText(MainActivity.this);
builder.setView(input);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
// ok stuff
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
// cancel stuff
}
});
builder.setOnDismissListener(new DialogInterface.OnDismissListener()
{
#Override
public void onDismiss(DialogInterface dialog)
{
//dismiss stuff
}
});
builder.show();
The problem here is that whenever user presses ok button, dismiss listener gets triggered right after.Is there any way to not trigger dismiss listener if user presses button?
I do realize that I can use a boolean flag, but I am hoping that there is actually an elegant solution.
I am not searching for solution on how to prevent dialog from being dismissed. I am searching for solution on how to prevent dismiss listener from being triggered when ok button is pressed and dialog gets dismissed.
I think what you need is setOnCancelListener().
setOnDismissListener() will be called for any reason. It means if dialog will disappear from the screen either due to Ok/Cancel button press or screen touch or back button or home button press, setOnDismissListener() will be called.
Sets the callback that will be called when the dialog is dismissed for
any reason.
If you are interested in listening for all cases where the dialog is
dismissed and not just when it is canceled, see setOnDismissListener
So workaround it what you have mentioned, check using some boolean flags and handle it.
You can again show that dialog after being dismissed!
You can try CustomViewDialog by using
LayoutInflater myDialog = getLayoutInflater();
View convertView = (View) myDialog.inflate(R.layout.MyLayoutXmlFile, null);
Also don't use positive or negative buttons only use buttons in the dialog layout.

setCancelable(false) and onBackPressed conflict

I have a problem
I have a non cancelable custom dialog
which mean this custom dialog can only close if presses buton inside the custom dialog, so it won't cancel on backpress or click outside
I tried setCancelable(false) and it works however in my activity I have a onBackPressed and whenever my non cancelable dialog show onBackPressed wont trigger when I click back button because I think they conflict
is their a solution to do this?
EDIT: The purpose is I want the user to click button ok, or skip inside the custom dialog which means this dialog is required before proceeding to next activity
also in onBackPressed since I am using fragment whenever user press back it changes to previous fragment
sorry for lacking of explanation
my code is this
Dialog
dialog_welcome_navigation = DialogUtils.showCustomDialog(context, R.layout.dialog_welcome_navigation);
dialog_welcome_navigation.setCancelable(false); // disable closing dialog with back pressed
dialog_welcome_navigation.setCanceledOnTouchOutside(true);
and the onBackPressed
#Override
public void onBackPressed(){
Log.d("TAG", "--back--");
}
After searching I have found a solution thanks to this SO answer
https://stackoverflow.com/a/25251122/3481654
I add a setOnKeyListener on my dialog
dialog_welcome_navigation.setOnKeyListener(dialogWelcomeNavigationOnKey);
private DialogInterface.OnKeyListener dialogWelcomeNavigationOnKey = new DialogInterface.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == android.view.KeyEvent.KEYCODE_BACK) {
dialog_welcome_navigation.dismiss();
// move other fragment
return true;
}
return false;
}
};

Alert dialog pops up even after dismissing it

I have come across a typical problem and it seems strange to me. Details are something like this - On the activity in my app, there are edittexts and submit button. After filling the data in the edittexts, user can click submit button. After clicking submit button, based on the values that are entered by the user, either of the two alert dialogs are shown. One is success and the other one is failed.
The thing is when the user enters invalid data and clicks submit button, the failed alert dialog gets opened. I have a button(OK) on the failed alert dialog, after clicking it I wrote dialog.dismiss(); to make it disappear, so that user can recheck the data and can modify. But the problem is while rechecking & modifying the data if he changes the orientation, then again the failed alert dialog is popping up even without clicking submit button. Please suggest.
Extra Details(though probably not necessary for this problem): While changing orientation the activity is recreated. So, I am saving the current data in the onSavedInstanceState() and retrieving it in onCreate() method to set back the values in the edittexts. Everything works fine, but once clicking on submit button, the respective alert dialog appears. Then after changing orientation the dialog is again popping up. I am sure that I wrote showDialog(1); in the onClick() method but then again why control is going back into onClick and showing that alert dialog even without clicking.
protected Dialog onCreateDialog(int id) {
switch(id){
case 0:
return new AlertDialog.Builder(this)
.setMessage("Success!")
.setIcon(R.drawable.success)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
case 1:
return new AlertDialog.Builder(this)
.setMessage("Failed")
.setIcon(R.drawable.failure)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
return;
}
}).show();
}
return null;
}
Here is the method that makes alert dialog show.
public void onClick(View v) {
switch (v.getId()) {
//Here there are other cases too.
case R.id.submit:
getEditTexts();
validator();
break;
}
}
public void validator() {
if(generator.receiveVal(0,0,sudo)) {
showDialog(0);
}
else if(!generator.receiveVal(0,0,sudo)) {
showDialog(1);
}
}
Just try replacing .create() in the place of .show(). In your case like this:
case 1:
return new AlertDialog.Builder(this)
.setMessage("Failed")
.setIcon(R.drawable.failure)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
return;
}
}).create(); //Here replaced .show with .create()
This is just an idea but it seems the issue is that onOrientation is trying to re-draw the activity.
Try something like the following:
You can add this to the activity declaration in the manifest:
android:configChanges="orientation"
so it looks like
<activity android:label="#string/app_name"
android:configChanges="orientation"
android:name=".your.package">
The matter is that the system destroys the activity when a change in the configuration occurs. See ConfigurationChanges.
So putting that in the configuration file avoids the system to destroy your activity. Instead it invokes the onConfigurationChanged(Configuration) method.
Hope this helps.

Categories

Resources