2011-11-03 96 views
0

我使用以下代碼完成一項活動,但我想完成後退新聞事件中的列表活動。幫我提前 感謝如何在設備上按下後退按鈕來完成listActivity

public void onBackPressed() { 
     AlertDialog alertDialog = new AlertDialog.Builder(this).create(); 
      alertDialog.setMessage("Want to exit ?"); 
      alertDialog.setButton("Yes", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        for(Activity activity:activities) 
         activity.finish(); 
      } }); 
      alertDialog.setButton2("No", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 

      }}); 
      alertDialog.show(); 
    } 
+0

(活動活動:活動) activity.finish(); - 您是否在應用程序中保留了一系列活動 – Optimus

回答

0

我不是修改你的代碼,只是我的建議在這裏新的代碼,

public boolean onKeyDown(int keyCode, KeyEvent event) { 
     // TODO Auto-generated method stub 

     if(keyCode==KeyEvent.KEYCODE_BACK){ 
     new AlertDialog.Builder(ProgressController.this).setTitle(
     "Attention").setMessage(
     "Want to exit ?") 
     .setPositiveButton("YES", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, 
          int which) { 
         finish(); 
        } 
       }).setNegativeButton("NO", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, 
          int which) { 

        } 
       }).show(); 
     return true; 
     }else{ 
      return false; 
     } 

     //return super.onKeyDown(keyCode, event); 

    } 
0

你的代碼應該修改如下圖所示..

@Override 
    public void onBackPressed() { 
     // TODO Auto-generated method stub 

     AlertDialog alertDialog = new AlertDialog.Builder(this).create(); 
     alertDialog.setMessage("Want to exit ?"); 
     alertDialog.setButton("Yes", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       finish(); 
     } }); 
     alertDialog.setButton2("No", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 

       return; 
     }}); 
     alertDialog.show(); 

    } 
+0

您的建議很有幫助,但與我的代碼所做的一樣。 – puneet

0

將一個取消偵聽器添加到您的警報對話框,並從那裏關閉您的活動。

alertDialog.setCancelable(true); 
    alertDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { 

     @Override 
     public void onCancel(DialogInterface dialog) { 
      finish(); 
     } 
    }); 
相關問題