简体   繁体   English

处理Android中的方向

[英]Handling Orientation in Android

How can I save the state of my tabs when the device Orientation is changed? 更改设备Orientation时,如何保存选项卡的状态? I have an ArrayList that holds all the currently open tabs 我有一个ArrayList ,它包含所有当前打开的选项卡

ArrayList<TabHost.TabSpec> mTabList = new ArrayList<TabHost.TabSpec>();

Changing the Orientation of the device clears all the tabs the user created. 更改设备的Orientation将清除用户创建的所有选项卡。

I would do it with savedInstanceState but I don't know how to bundle it, if that's even possible. 我会用savedInstanceState做,但我不知道如何捆绑它,如果这是可能的。

What are your suggestions? 你有什么建议?

In the manifest for your activity that contains the list in question add configChanges attribute to it. 在包含相关列表的活动清单中,为其添加configChanges属性。 For the value of the attribute set it to "orientation". 对于属性的值,将其设置为“orientation”。 This should stop the default behavior of restarting the activity which is why your list is getting cleared. 这应该会停止重新启动活动的默认行为,这就是清除列表的原因。

Hey Buddy just put in the manifest file 嘿Buddy刚刚放入清单文件

<activity android:name="YourActivityName" android:configChanges="orientation|keyboardHidden"></activity>

Just keep in mind 请记住

android:configChanges="orientation|keyboardHidden"

if the above solution isn't work for you in your case then you can use 如果上述解决方案在您的情况下不适合您,那么您可以使用

Saving Android Activity state using Save Instance State 使用“保存实例状态”保存Android活动状态

Hope this will work... 希望这会奏效......

If your requirement is to save the state of the application on orientation without stopping the default behaviour on orientation changed,refer this Reference 如果您的要求是在方向上保存应用程序的状态而不停止更改方向的默认行为,请参阅此参考

you can use savedInstanceState.putParcelableArrayList(key, value)..The whole idea would depend on whether you want to load the resoures again on orientation change say if your using different layout resources for landscape and portrait modes .otherwise handling using android:configChanges="orientation" would be better choice 你可以使用savedInstanceState.putParcelableArrayList(键,值)..整个想法将取决于你是否想要在方向更改时再次加载资源,如果您使用不同的布局资源用于横向和纵向模式。另外使用android处理:configChanges = “定位”将是更好的选择

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM