Issues in onBackpressed() - java

Actually my issue is when i press the back button it shows a dialog and also at the same time the application finished without any operation on key? Can any one solve this?
#Override
public void onBackPressed() {
super.onBackPressed();
AlertDialog.Builder ab = new AlertDialog.Builder(this);
ab.setMessage("Really want to exit");
ab.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
ab.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Main.super.onBackPressed();
}
});
ab.create();
ab.show();
}

remove super.onBackPressed(); from method, keep it in onClick() only
#Override
public void onBackPressed() {
super.onBackPressed(); //remove it
}
seems you are performing same operation in both buttons, you should remove Main.super.onBackPressed() from negativeButton
ab.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
super.onBackPressed();
}
});
ab.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
ab.dismiss();
}
});

Don't call super method of onBackPressed.
#Override
public void onBackPressed() {
AlertDialog.Builder ab = new AlertDialog.Builder(this);
ab.setMessage("Really want to exit");
ab.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
ab.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
ab.create();
ab.show();
}

try this
#Override
public void onBackPressed() {
AlertDialog.Builder ab = new AlertDialog.Builder(this);
ab.setMessage("Really want to exit");
ab.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
ab.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Main.super.onBackPressed();
}
});
ab.create();
ab.show();
}

You need to Remove, super.onBackPressed(); from onBackPressed(). then your function should as,
#Override
public void onBackPressed() {
AlertDialog.Builder ab = new AlertDialog.Builder(this);
ab.setMessage("Really want to exit");
ab.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
ab.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Main.super.onBackPressed();
}
});
ab.create();
ab.show();
}

Related

AlertDialog with EditText and Three buttons

So i have tis code and i'm trying to create a AlertDialog with an EditTet and Three buttons the positive one, the négative one and the neutral one , but it doesn't work and the application stops
b5.setOnClickListener(new View.OnClickListener() {
#SuppressLint("UseCompatLoadingForDrawables")
#Override
public void onClick(View view) {
AlertDialog.Builder boite;
boite = new AlertDialog.Builder(MainActivity.this);
boite.setTitle("boite de dialogue");
boite.setIcon(getDrawable(R.drawable.warning_shield_96px));
final EditText input = new EditText(MainActivity.this);
input.setInputType(InputType.TYPE_CLASS_TEXT);
boite.setView(input);
boite.setPositiveButton("OUI", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
//whatever action
}
});
boite.show();
boite.setNegativeButton("NON", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
//whatever action
}
});
boite.show();
boite.setNeutralButton("CANCEL", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
//whatever action
}
});
boite.show();
}
});
There is no need to call boite.show() several times, just call it once like below :
b5.setOnClickListener(new View.OnClickListener() {
#SuppressLint("UseCompatLoadingForDrawables")
#Override
public void onClick(View view) {
AlertDialog.Builder boite;
boite = new AlertDialog.Builder(MainActivity.this);
boite.setTitle("boite de dialogue");
boite.setIcon(getDrawable(R.drawable.warning_shield_96px));
final EditText input = new EditText(MainActivity.this);
input.setInputType(InputType.TYPE_CLASS_TEXT);
boite.setView(input);
boite.setPositiveButton("OUI", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
//whatever action
}
});
boite.setNegativeButton("NON", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
//whatever action
}
});
boite.setNeutralButton("CANCEL", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
//whatever action
}
});
boite.show();
}
});
AlertDialog uses Builder Pattern to initialize, so you can set different methods and buttons and anything you like, then when you call alertDialog.show() it builds the object with any configs you set before that call.

Issue with alert dialogue into my android app

I added an alert dialogue into my app and i want to user when he press yes he should exit from the complete app but it dose not. by pressing yes i just come back to previous activity
here is code
#Override
public void onBackPressed() {
new AlertDialog.Builder(Chapter1.this)
.setTitle("Exit")
.setIcon(R.drawable.ic_baseline_warning_24)
.setMessage("Are you sure you want to go back")
.setPositiveButton("yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
}).setNeutralButton("Help", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Chapter1.this, "This is an \"Android Studio\" learning app", Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
can any one tell me which kind of change in code should i made to that when i press yes i should leave the app instead of going to previous activity.
finish();
also try looking at the response that was provided here How to quit android application programmatically you will get some clue.
#Override
public void onBackPressed() {
new AlertDialog.Builder(Chapter1.this)
.setTitle("Exit")
.setIcon(R.drawable.ic_baseline_warning_24)
.setMessage("Are you sure you want to go back")
.setPositiveButton("yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finishAffinity();
System.exit(0);
}
}).setNeutralButton("Help", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Chapter1.this, "This is an \"Android Studio\" learning
app", Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();

Inflating AlertDialog inside PositiveButton causes last dialog to not dismiss

I'm trying to open 2 alert dialogs like permissions, the first one pops up and dismiss when I press the positiveButton, then It inflates the second one, but when I press positiveButton on second alertdialog it does not dosmiss and keeps showing it
private void requestXiaomiBootPermission(){
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Automatic boot");
builder.setMessage("we need this permission to work");
builder.setCancelable(false);
builder.setPositiveButton("Allow", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int i) {
addAutoStartup();
requestXiaomiBackgroundPermission();
dialog.dismiss();
}
}).show();
builder.setNegativeButton("Deny", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
builder.setNeutralButton("do not show", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
builder.show();
}
private void requestXiaomiBackgroundPermission(){
AlertDialog.Builder builder2 = new AlertDialog.Builder(MainActivity.this);
builder2.setTitle("Popup window");
builder2.setMessage("we need this permission to work");
builder2.setCancelable(false);
builder2.setPositiveButton("Allow", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int i) {
addPermissionsOnBackgroud();
dialog.dismiss(); // --> Here it does not dismiss when this dialog inflates
}
}).show();
builder2.setNegativeButton("Deny", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
builder2.setNeutralButton("do not show", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
builder2.show();
}
So the circuit is this
I open the app and the first dialog pops up ( requestXiaomiBootPermission() ) , then when I press the positiveButton allow it opens the second dialog ( requestXiaomiBackgroundPermission() ) but when I press allow on this one, dialog.dismiss() does not dismiss my dialog and keeps there, any solution?
Don't invoke show() after every setXXXXXButton() call. Just invoke it once, at the end. You're creating 4 boot permission dialogs, and 4 background permission dialogs, and they're all getting stacked on top of each other.
You are opening both dialog 4 times. Remove show() method after all three button.just keep show() method at end.
private void requestXiaomiBootPermission(){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Automatic boot");
builder.setMessage("we need this permission to work");
builder.setCancelable(false);
builder.setPositiveButton("Allow", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int i) {
dialog.dismiss();
}
});
builder.setNegativeButton("Deny", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.setNeutralButton("do not show", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}

How to set AlertDialog.finish() in the AlertDialog?

I am currently using an app that needs to utilize a lot of AlertDialogs. I've currently coded a basic one here:
protected void StopButton () {
AlertDialog.Builder StopDialog = new AlertDialog.Builder(MainActivity.this);
StopDialog.setTitle(R.string.Stop_Title);
StopDialog.setMessage(R.string.Stop_Message);
StopDialog.setPositiveButton(R.string.Yes_Button, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
((Protoype2) getApplication()).setRequestingLocationUpdates(false);
finish();
}
});
StopDialog.setNegativeButton(R.string.No_Button, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
((Protoype2) getApplication()).setRequestingLocationUpdates(true);
}
});
StopDialog.setNeutralButton(R.string.Negative_Button, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//Closes box
finish();
}
});
AlertDialog alert = StopDialog.create();
alert.show();
}
The StopButton works and the Dialog comes up when I call for it. However, the finish(); function does not work.
Upon review, I found that finish(); did not finish the Dialog, rather the entire app. I know I need to get a AlertDialog.cancel in there.
The problem is this: as you can see, the AlertDialog is only created AFTER the StopDialog is finished.
How can I set an AlertDialog.finish() before StopDialog is finished?
Replace finish() with dialog.dismiss() like this:
AlertDialog.Builder StopDialog = new AlertDialog.Builder(TestActivity.this);
StopDialog.setTitle("Title");
StopDialog.setMessage("Stop");
StopDialog.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
((Protoype2) getApplication()).setRequestingLocationUpdates(false);
dialog.dismiss();
}
});
StopDialog.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
((Protoype2) getApplication()).setRequestingLocationUpdates(true);
}
});
StopDialog.setNeutralButton("Neutral", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//Closes box
dialog.dismiss();
}
});
AlertDialog alert = StopDialog.create();
alert.show();
There is no such method finish() for AlertDialog. finish() refers to Activity class and will finish the current Activity. You should instead use dismiss().
You can use the DialogInterface object like dialog.dismiss() to dismiss the AlertDialog.
You can go through the documentation for a broader idea :)
http://developer.android.com/guide/topics/ui/dialogs.html

Is Possible for on Back Pressed method with alert dialog in fragment

#Override
public void onBackPressed() {
Log.d("back button", "back button pressed");
AlertDialog.Builder ad1=new AlertDialog.Builder(getActivity());
ad1.setMessage("Are you sure you want to exit? ");
ad1.setCancelable(false);
ad1.setPositiveButton("YES", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg0, int arg1) {
Intent i = new Intent(getActivity(), LoginActivity.class);
startActivity(i);
}
});
ad1.setNegativeButton("NO", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg0, int arg1) {
Intent i = new Intent(getActivity(), FrndsearchFragment.class);
startActivity(i);
}
});
AlertDialog alert=ad1.create();
alert.show();
}
By using the code i getting the error in #Override and i write the super.onBackPressed i got onBackPressed in super.onBackPressed .
Yes you can do the same by overriding onKeyDown method.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setTitle("Exit");
builder.setMessage("Do you want to exit the application?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
exit();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
return true;
}
return super.onKeyDown(keyCode, event);
}
private void exit() {
finish();
}

Categories

Resources