Android的方向变化要求的onCreate方向、Android、onCreate

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

我做了有一个选项卡中的关键字,过滤器和搜索按钮,和三个可选选项卡的不同类型的结果(每片含一个的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" /> 

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

阅读全文

相关推荐

最新文章