問題描述
旋轉屏幕后如何恢復對話框等?例如,彈出一個 alertDialog 告訴用戶一些信息.然后用戶將屏幕旋轉到另一個方向.如何恢復alertDialog?任何人都可以指導我這樣做嗎?謝謝!
How to restore the dialog etc after rotating the screen? For example, pop up an alertDialog to tell user some information. then the user rotate the screen to another orientation. How to restore the alertDialog? Can any one guide me to do it? Thanks!
稍后添加:
我查看了android源代碼并找到了這些東西:
I looked into the android source code and find these things:
對話框存儲在mManagedDialogs
中,相關信息為:
Dialogs are stored in mManagedDialogs
, and the related information is:
mManagedDialogs = new SparseArray<ManagedDialog>();
onSaveInstanceState
相關:
final void performSaveInstanceState(Bundle outState) {
onSaveInstanceState(outState);
saveManagedDialogs(outState);
}
在saveManagedDialogs
中,與mManagedDialogs
有關.
onRestoreInstanceState
相關:
final void performRestoreInstanceState(Bundle savedInstanceState) {
onRestoreInstanceState(savedInstanceState);
restoreManagedDialogs(savedInstanceState);
}
在restoreManagedDialogs
中,與mManagedDialogs
有關.
如您所見,對于高級功能,您必須自己完成保存和恢復工作.當您擁有大量自定義對話框時,這可能是一場噩夢.我沒有嘗試過復雜的對話框(例如輸入了 EdiText、listView).通過這種方式,我想警告用戶:在對話框中輸入信息時切勿旋轉屏幕......或者,在顯示對話框時動態鎖定旋轉.
As you can see, for advanced feature, you must do the saving and restoring job by yourself. It may be a night mare when you have tons customized dialogs. I have not tried the complex dialog (has input EdiText, listView, say). In this way, I'd like to warn users: Never rotate the screen when inputing your info in the dialog... OR, lock the rotation dynamically when showing the dialog.
感謝所有回答我的人.希望我的信息也能幫到你.
Thanks for all the people who answered me. Hope my information help you too.
推薦答案
我采取的方法是不允許操作系統在布局配置更改后重新啟動您的活動.為此,請在清單文件中要阻止重新啟動的活動中添加此行:
The approach I took was to not allow the OS to restart your activity after the layout configuration change. To accomplish this, add this line within activities that you want to prevent from restarting in your manifest file:
<activity
android:configChanges="orientation|keyboard"
...
>
您可以選擇在代碼中處理配置更改,以防您想要手動進行一些布局更改,例如從 XML 重新加載新視圖.這是通過覆蓋 Activity 類中的 onConfigurationChanged() 方法來完成的:
Optionally, you can handle the configuration change in code in case there are some layout changes you want to make manually, such as reloading a new view from XML. This is done by overwriting the onConfigurationChanged() method in your Activity class:
@Override
public void onConfigurationChanged(Configuration newConfig)
{
//Handle config changes here, paying attention to
//the newConfig.orientation value
super.onConfigurationChanged(newConfig);
}
在配置更改列表中添加了|keyboard"
Added "|keyboard" to the list of config changes
這篇關于Android:旋轉更改后對話框等恢復的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!