アンドロイドデバイスの戻るボタンを押すと警告メッセージが表示され、確実にユーザーの天気を尋ねます。ユーザーが「はい」を押すと、前のページに移動する必要があります。 「いいえ」の場合はアクティビティを再開する必要があります。しかし、私は両方のアクションを実行する戻るボタンを押すと問題が発生します。また、以前のアクティビティに移動し、警告ダイアログを表示します。ここに私のコードです。見て、私を案内してください..事前に感謝します。戻るボタンを押したときに警告ダイアログが表示される
public void onBackPressed() {
super.onBackPressed();
AlertDialog.Builder alertdialog=new AlertDialog.Builder(this);
alertdialog.setTitle("Warning");
alertdialog.setMessage("Are you sure you Want to exit the tutorial???");
alertdialog.setPositiveButton("yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent=new Intent(secAddition.this,addition.class);
startActivity(intent);
secAddition.this.finish();
}
});
alertdialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert=alertdialog.create();
alertdialog.show();
}}
はBackPressにstartActivityを呼び出してはいけません。 super OnBackPressを呼び出す必要があります。ハードウェアのバックが押されたとき。私の答えをチェックしてください。 –