2012-04-01 66 views
0

對不起,再次問這個問題,但我嘗試了幾個解決方案在stackoverflow和其他android組,但他們都沒有爲我工作。AlertBox調用意圖沒有點擊正面按鈕

在活動onBackpressed中,我想顯示一個警告框,要求用戶「退出應用程序?」如果用戶點擊是,我想要啓動我的應用程序的啓動器(主頁)活動,否則請單擊否按鈕保留在同一頁面上。 問題是顯示警告框後,我自動重定向到發射器(家庭)活動之前,我自動按YES鍵

plz幫助

CODE:

public void onBackPressed() { 
// TODO Auto-generated method stub 
super.onBackPressed(); 
Log.i(TAG, "BACK PRESSED"); 
AlertDialog.Builder renameDialog = new AlertDialog.Builder(MainMenu.this); 
renameDialog.setTitle("Warning"); 
renameDialog.setMessage("Do you want to logout?"); 
renameDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
public void onClick(DialogInterface arg0, int arg1) { 
launchIntent(); 
} 
}); 

renameDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
public void onClick(DialogInterface arg0, int arg1) { 
arg0.cancel();  
} 
}); 
renameDialog.show(); 
} 

private void launchIntent() { 
Log.i("positive button","pressed"); 
Intent i=new Intent(MainMenu.this,LoginActivity.class); 
i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); 
Log.e("from main menu","calling login"); 
startActivity(i); 
} 
+0

你究竟想要做什麼 – Ishu 2012-04-02 02:57:40

回答

3

你需要移動super.onBackPressed();內正面按鈕的點擊監聽器,因爲它只會完成正在運行的活動。或者,如果您願意,只需評論此行(排除它),因爲您當前的邏輯顯示,如果單擊肯定按鈕,您正在嘗試啓動新活動。

+0

我第二次Waqas的回答... super.onBackPressed()不需要...假設你在launchIntent()方法中寫入了一個startActivity(homeIntent)。 super.onBackPressed()完成當前活動,然後它可以調用dialog.show() – drulabs 2012-04-01 20:21:25

0

不要調用super.onBackPressed()!