2012-05-29 54 views
2

我有一個活動「DashboardActivity」(不是主要活動),我想要捕獲BACK按鈕以詢問他們是否要註銷(因爲BACK通常會將您帶到登錄屏幕) 。onBackPressed()沒有被調用

我已經試過

public void onBackPressed() { 
    // ask if they want to logout..... 
    logOutIfTheyReallyWantTo(); 

    //super.onBackPressed(); 
} 

但在調試器中測試這一點,似乎並沒有在所有的打碼。

然後我試圖添加

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ECLAIR 
      && keyCode == KeyEvent.KEYCODE_BACK 
      && event.getRepeatCount() == 0) { 
     // Take care of calling this method on earlier versions of 
     // the platform where it doesn't exist. 
     onBackPressed(); 
    } 

    return super.onKeyDown(keyCode, event); 
} 

這也證明了是浪費時間。

最後我想

@Override 
public boolean onKeyLongPress(int keyCode, KeyEvent event) { 
    if (keyCode == KeyEvent.KEYCODE_BACK) { 
     // a long press of the call key. 
     // do our work, returning true to consume it. by 
     // returning true, the framework knows an action has 
     // been performed on the long press, so will set the 
     // canceled flag for the following up event. 
     return true; 
    } 
    return super.onKeyLongPress(keyCode, event); 
} 

但調試檢測沒有​​。 logcat中只顯示

05-29 12:22:59.040: D/NetdConnector(14366): RCV <- {217 0} 
    05-29 12:22:59.040: D/NetdConnector(14366): RSP <- {217 0} 
    05-29 12:23:04.743: V/processReply(32759): sr0857000000 
    05-29 12:23:07.673: W/KeyCharacterMap(32759): No keyboard for id 0 
    05-29 12:23:07.673: W/KeyCharacterMap(32759): Using default keymap: /system/usr/keychars/qwerty.kcm.bin 
    05-29 12:23:07.907: V/Dashboard(32759): Pause 
    05-29 12:23:07.915: I/ComfortActivity(32759): start 
    05-29 12:23:07.915: V/ComfortActivity(32759): Resume 
    05-29 12:23:08.032: D/dalvikvm(14430): GC_EXPLICIT freed 92K, 52% free 3322K/6791K, external 5633K/7003K, paused 77ms 
    05-29 12:23:08.329: I/DashboardActivity(32759): stop 
    05-29 12:23:08.829: V/processReply(32759): sr0856000000 

似乎有大約這許多問題,到目前爲止,我已經嘗試了所有的建議,我可以找到,但沒有運氣。

----在答覆問題....

void logOutIfTheyReallyWantTo() 
{ 
    AlertDialog.Builder alert = new AlertDialog.Builder(this); 
    alert.setTitle("Log out?"); 
    alert.setMessage("Do you really want to log out?"); 

    //AlertDialog loginPrompt = alert.create(); 

    alert.setPositiveButton("OK", new DialogInterface.OnClickListener() 
    { 

     public void onClick(DialogInterface dialog, int which) { 
      ((ComfortApp) getApplicationContext()).soundClick(); 

      getComms().logout(true); 
      Intent intent = new Intent(DashboardActivity.this, ConfigurationActivity.class); 
      startActivityForResult(intent, 0); 
     } 
    }); 
    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
    { 

     public void onClick(DialogInterface dialog, int which) { 
      // TODO Auto-generated method stub 
      // just like Perry the Platypus.... 
      // don't do much 
      ((ComfortApp) getApplicationContext()).soundClick(); 
     } 
    }); 
    alert.show(); 

} 
+0

愚蠢的問題,但如果是在Java代碼中的logcat的語句? – Sam

+0

嘗試替換返回super.onKeyLongPress(keyCode,event); with「return false」 –

+0

請將代碼發佈到'logOutIfTheyReallyWantTo();'因爲我無法想到onBackPressed()沒有執行的原因。 – Sam

回答

0

嗨請嘗試以下代碼..

@Override 
    public void onBackPressed() { 
     AlertDialog.Builder alertbox = new AlertDialog.Builder(this); 
     alertbox.setMessage("Are you sure logout?"); 
     alertbox.setTitle("Logout"); 
     alertbox.setNeutralButton("Yes", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface arg0, int arg1) { 
       showDialog(0); 
       new Thread() { 
        public void run() { 
         startActivity(new Intent(DashBoard.this, Login.class)); 
         DashBoard.super.onBackPressed(); 
        } 
       }.start(); 
      } 
     }); 
     alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface arg0, int arg1) { 
      } 
     }); 
     alertbox.show(); 
    } 
+0

很久以後歡迎回來:) –

+0

這幾乎是代碼所做的 - 但它永遠不會被調用。 –

1

覆蓋finish()活動中捕捉退出。如果你不想退出,請不要撥打super

@Override 
    public void finish() 
    { 
     if(!wantToExit) 
     { 
      super.finish(); 
     } 
     else 
     { 
      logOutIfTheyReallyWantTo(); 
     } 
    } 

onBackPressed()Activity默認實現 -

public void onBackPressed() 
    { 
     finish(); 
    } 
+0

沒有試圖阻止它退出我不在乎它是否退出我只是想停止從登錄到顯然退出(但仍然登錄)的特定屏幕轉換 –

+0

您仍然可以捕獲在'finish( )' – Shaiful