机器人:对话等恢复旋转而改变后机器人

2023-09-06 03:05:24 作者:崩溃

旋转屏幕后如何​​恢复对话等?例如,弹出一个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源$ C ​​$ c和找到这些东西:

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.

推荐答案

我带是不允许的操作系统布局配置更改后重新启动活动的方式。要做到这一点,在要prevent重新启动您的manifest文件活动中加入这一行:

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"
 ...
 >

另外,您可以处理code的配置变化的情况下也有要手动进行,如重装从XML的新观点的一些布局的变化。这是通过重写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);
}

编辑:添加|键盘的配置变化列表

Added "|keyboard" to the list of config changes