Android 方向更改调用 onCreate [英] Android orientation change calls onCreate

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

问题描述

我制作了一个搜索屏幕,其中有一个用于关键字、过滤器和搜索按钮的选项卡,以及三个用于不同类型结果的可选选项卡(每个选项卡都包含一个带有 ListViewListView>ArrayAdapter).启动 Activity 时,如果已经执行了搜索,开发人员可以选择将结果作为额外的 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 的自动方向切换支持似乎会重新创建整个 Activity,调用 onCreate().这意味着我的搜索结果将重置为启动 Activity 时传递的 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.

到目前为止,我唯一的解决方案是先调用 finish() 然后调用 startActivity() 以使用新结果重新启动活动.我确信一定有一个更简单的解决方案,而且我做了一些非常菜鸟的事情.

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 属性添加到您的 AndroidManifest.xml 中,如下所示:

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

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

Activity 在轮播 Android 上重启如何在 Android 上禁用方向更改?http://developer.android.com/guide/topics/manifest/activity-element.html#config

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

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