2011-11-08 97 views
1

我有一個AlertDialog.Builder在應用程序上顯示。當我旋轉屏幕時,我得到「應用程序已泄漏窗口」的錯誤。如何取消onPause()事件中的AlertDialog?我沒有看到Ad.cancel()的任何方法。屏幕旋轉時取消AlertDialog

更新:爲工作模型編輯的代碼。

private AlertDialog inputDlg; 
    ... 
    @Override 
    protected void onPause() 
    { 
    // close dialog if it's showing 
    if (inputDlg != null) 
     if (inputDlg.isShowing()) 
      inputDlg.dismiss(); 

     super.onPause(); 
    } 

    ... 

    private void dlgUserPrompt (Context c) 
    { 
     // Set an EditText view to get user input 
     final EditText input = new EditText (c); 

     AlertDialog.Builder Ab = new AlertDialog.Builder (this); 
     Ab.setTitle("title"); 
     Ab.setMessage("I won't explode if you rotate this"); 
     Ab.setView(input); 
     inputDlg = Ab.show(); 
    } 

回答

1

我認爲你正在尋找dismiss方法。

+0

實際上,當使用AlertDialog.Builder時,你不會得到一個簡單的解僱方法。在閱讀了Kurtis的回覆之後,看起來解決方案實際上非常複雜。 – wufoo

+1

AlertDialog.Builder上的show()方法返回確實具有解除方法的AlertDialog對象。除非您在視圖中進行的計算密集程度非常高,否則最好讓Android在配置更改中殺死/重新啓動您的活動,而不是在清單中重寫此操作,因爲這是更簡單的解決方案。 – Chris

+0

謝謝克里斯。這讓我走上了正軌。上面更新的代碼。 – wufoo

0

您需要覆蓋onConfigurationChange()鉤子來執行此操作。這document將幫助更多地瞭解配置更改是什麼以及如何處理它們。