Android的方向变化要求的onCreate [英] Android orientation change calls onCreate

查看:111
本文介绍了Android的方向变化要求的onCreate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了有一个选项卡中的关键字,过滤器和搜索按钮,和三个可选选项卡的不同类型的结果(每片含一个的ListView搜索画面 ArrayAdapter )。当开始活动,显影剂可以任选通过在结果作为额外Parcelable []如果搜索已经执行。在的onCreate()方法,我创建这三个选项卡的 Parcelable [] 通过。

I've made a search screen that has one tab for keywords, filters, and a search button, and three optional tabs for the different types of results (each containing a ListView with an ArrayAdapter). When starting the activity, the developer can optionally pass in the results as an extra Parcelable[] if the search has already been performed. In the onCreate() method I'm creating each of the three tabs for the Parcelable[] passed through.

当我打电话从过滤器选项卡上的按钮进行搜索,我清楚的标签,并与新的结果,这完美的作品重新创建。问题是,当你旋转设备,看来Android的自动定位切换支持再现了整个活动,号召的onCreate()。这意味着,我的搜索​​结果都将重置为 Parcelable [] 启动活动时,通过。

When I call a search from the button on the filter tab, I clear the tabs and recreate them with the new results, which works perfectly. The problem is that when you rotate the device, it appears that Android's automatic orientation switching support recreates the entire activity, calling onCreate(). This means that my search results are reset to the Parcelable[] passed through when starting the activity.

我已经到目前为止唯一的解决方法是调用完成()然后 startActivity()基本上重新启动与新成果的活动。我敢肯定,必须有一个更简单的解决办法,我已经做了一些非常noobish。

The only solution I've had so far is to call finish() then startActivity() to essentially restart the activity with the new results. I'm sure there must be a much simpler solution and that I've done something extremely noobish.

有没有更好的方式来做到这一点?

Is there a better way to do this?

推荐答案

课程教材中存在。只需添加configChanges属性到您的Andr​​oidManifest.xml,这样的:

Of cource there is. Just add configChanges attribute to your AndroidManifest.xml, like that:

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

<一个href="http://stackoverflow.com/questions/456211/activity-restart-on-rotation-android">http://stackoverflow.com/questions/456211/activity-restart-on-rotation-android <一href="http://stackoverflow.com/questions/1512045/how-to-disable-orientation-change-in-android">http://stackoverflow.com/questions/1512045/how-to-disable-orientation-change-in-android <一href="http://developer.android.com/guide/topics/manifest/activity-element.html#config">http://developer.android.com/guide/topics/manifest/activity-element.html#config

这篇关于Android的方向变化要求的onCreate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆