2015-03-19 151 views
0

DialogFragment的創建生命週期中的哪個點可以放心地關閉它,以便它永遠不會被用戶看到?我的意圖是,當用戶旋轉屏幕時,DialogFragment會再次經歷創建生命週期,我寧願選擇性地重新創建它。如果condition x,請重新創建DialogFragment,如果condition y,只是在它仍然被重新創建時解僱它,以便用戶永遠不會在新的方向上看到它。那麼,我可以放心地說,dismiss在創建對話框片段時忽略對話框片段

+1

你可以檢查它是否顯示之前打電話解僱? – Blackbelt 2015-03-19 09:48:25

+0

@Blackbelt,我會在那裏放? 'onViewCreated'? – Aks 2015-03-19 09:50:34

+0

或者你可以在顯示對話框 – Blackbelt 2015-03-19 09:54:28

回答

0

你可以嘗試控制DialogFragment生活在onConfigureChanged(...)

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 

    Fragment yourDialog = getFragmentManager().findFragmentByTag("dialog_tag"); 
    if(yourDialog != null) { 
     DialogFragment df = (DialogFragment) yourDialog; 
     df.dismiss(); 
    } 

    if(condition x) 
    { 
     //here recreate your dialog 
    } 
} 

希望這種想法的幫助!

0

您應該參考this瞭解Dialog Fragment的詳細文檔。

欲瞭解更多信息,請參閱this。希望這可以幫助你。