2016-05-17 16 views
0

アンドロイドデバイスの戻るボタンを押すと警告メッセージが表示され、確実にユーザーの天気を尋ねます。ユーザーが「はい」を押すと、前のページに移動する必要があります。 「いいえ」の場合はアクティビティを再開する必要があります。しかし、私は両方のアクションを実行する戻るボタンを押すと問題が発生します。また、以前のアクティビティに移動し、警告ダイアログを表示します。ここに私のコードです。見て、私を案内してください..事前に感謝します。戻るボタンを押したときに警告ダイアログが表示される

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(); 



}} 
+0

はBackPressにstartActivityを呼び出してはいけません。 super OnBackPressを呼び出す必要があります。ハードウェアのバックが押されたとき。私の答えをチェックしてください。 –

答えて

3

に行くので、私は次のコードスニペットを確認してください

0123事のslimier種類を実装呼び出されます
1

これは動作するはず

super.onBackPressed(); 

を削除...

public void 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(); 



}} 
+0

ありがとう –

+0

親切にマークとして回答。 –

5

、のようなあなたはアプリを閉じたり、スタックをクリアせずに警告ダイアログを表示したい場合は、電話しないでください super.onBackPressed();

+0

それは働いた。ありがとう –

1

あなたはあなたのコードが動作するためには、次のように書くことができます。

public void 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) { 
     super.onBackPressed();  
    } 
}); 

alertdialog.setNegativeButton("No", new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 
     dialog.cancel(); 
    } 
}); 


AlertDialog alert=alertdialog.create(); 
alertdialog.show(); 

} 
} 

Activityしようとしている再び場合Activityを起動する必要はありません、

をあなたのコードでは、あなたが戻ってプレスを行う際に、 super.onBackPressed()は、それが前Activity

2

これはあなたが探しているものです。あなたはバックプレスでstartActivityを押す必要はありません。 ユーザーが[はい]を選択すると呼び出すよりも、スーパーonBackPress

public void 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) { 

    super.onBackPressed(); 

} 
}); 

alertdialog.setNegativeButton("No", new DialogInterface.OnClickListener() { 
@Override 
public void onClick(DialogInterface dialog, int which) { 
    dialog.cancel(); 
} 
}); 


AlertDialog alert=alertdialog.create(); 
alertdialog.show(); 
}} 
関連する問題