按下后退按钮时如何显示警报消息?
How to display Alert Message when back button is Pressed?
我需要在用户按下 Back Button
时向他显示一个 Alert Message
。
我写了下面的代码,它显示 Alert Box
一秒钟然后重定向。我在 Alert Box
中指定了 Button
,如果用户 click
在 yes
上,那么我创建 Intent
,否则我希望用户留在当前 [=26] =].
现在它向我显示以下错误消息。
android.view.WindowLeaked: Activity checkout.Checkout has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{100e8093 V.E..... R.....I. 0,0-1080,476} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:363)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:271)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
at android.app.Dialog.show(Dialog.java:298)
at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:953)
代码。
@Override
public void onBackPressed() {
super.onBackPressed();
AlertDialog.Builder alertDialog2 = new AlertDialog.Builder(this);
alertDialog2.setTitle("Delete Data");
alertDialog2.setMessage("Are you sure you want to go back?.");
// Add the buttons
alertDialog2.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
//Here i am doing JSON Stuff
JSONObject commandData = new JSONObject();
}
});
alertDialog2.show();
评论这一行:
super.onBackPressed();
@Override
public void onBackPressed() {
AlertDialog.Builder alertDialog2 = new AlertDialog.Builder(this);
alertDialog2.setTitle("Delete Data");
alertDialog2.setMessage("Are you sure you want to go back?.");
// Add the buttons
alertDialog2.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
//Here i am doing JSON Stuff
JSONObject commandData = new JSONObject();
}
});
alertDialog2.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
YourClass.super.onBackPressed();
}
};
alertDialog2.show();
我需要在用户按下 Back Button
时向他显示一个 Alert Message
。
我写了下面的代码,它显示 Alert Box
一秒钟然后重定向。我在 Alert Box
中指定了 Button
,如果用户 click
在 yes
上,那么我创建 Intent
,否则我希望用户留在当前 [=26] =].
现在它向我显示以下错误消息。
android.view.WindowLeaked: Activity checkout.Checkout has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{100e8093 V.E..... R.....I. 0,0-1080,476} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:363)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:271)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
at android.app.Dialog.show(Dialog.java:298)
at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:953)
代码。
@Override
public void onBackPressed() {
super.onBackPressed();
AlertDialog.Builder alertDialog2 = new AlertDialog.Builder(this);
alertDialog2.setTitle("Delete Data");
alertDialog2.setMessage("Are you sure you want to go back?.");
// Add the buttons
alertDialog2.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
//Here i am doing JSON Stuff
JSONObject commandData = new JSONObject();
}
});
alertDialog2.show();
评论这一行:
super.onBackPressed();
@Override
public void onBackPressed() {
AlertDialog.Builder alertDialog2 = new AlertDialog.Builder(this);
alertDialog2.setTitle("Delete Data");
alertDialog2.setMessage("Are you sure you want to go back?.");
// Add the buttons
alertDialog2.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
//Here i am doing JSON Stuff
JSONObject commandData = new JSONObject();
}
});
alertDialog2.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
YourClass.super.onBackPressed();
}
};
alertDialog2.show();