Android TabActivity 和方向更改
我有一个 TabActivity
托管其他三个活动。
如果选择第一个选项卡,我可以更改设备方向,一切正常。
如果我转到第二个或第三个选项卡并更改方向,选项卡子活动(第二个和第三个)的 onCreate()
方法将被调用两次,第一次是设置默认选项卡内容(第一个)和第二个 onCreate 正在设置选定的选项卡内容(第一次应该如此)。
为什么 onCreate()
方法被调用两次,我该如何解决这个问题?
编辑:
我不想在清单中使用 android:configChanges="orientation"
因为我想将应用程序标题和系统栏向下并纵向显示以显示它们,而这只有可能在设置内容之前...
I have a TabActivity
that hosts other three activities.
If the first tab is selected, I can change the device orientation and everything is ok.
If I go to the second or the third tab and change the orientation, the tab sub activity's(second and third) onCreate()
method gets called twice, for the first time is setting the default tab content(the first one) and at the second onCreate, is setting the selected tab content(as it should for the first time).
Why is onCreate()
method being called twice and how can I solve the problem?
EDIT:
I don't want to use android:configChanges="orientation"
in the manifest because I want to get the app title and system bar down and on portrait to show them, and this is possible only before setting a content...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用清单文件中的
android:configChanges="orientation"
属性,如下所示use the
android:configChanges="orientation"
attribute in manifest file like below为了让我清楚,您是说您有多个选项卡,它们使用不同的方向(纵向或横向),并且您在切换选项卡并正确设置相应方向时遇到问题?
回应 Cata 的评论:
是的,所以每当您旋转屏幕时,当前可见的活动都会被销毁,并再次调用 onCreate (如果我记得这些步骤)。您要做的就是调用 getCurrentTab(),它返回代表选项卡的 int 值,并在调用 onCreate 时将其重置为活动选项卡。您可以通过多种方式执行此操作...通过使用一个小方法来处理该问题并通过 onCreate 调用它,或者使用 onSaveInstanceState(Bundle) 保存当前数据,并使用 onRestoreInstanceState() 重新加载选项卡数据。
您可以设置一个全局 int (int currentTab = 0),而不是在 onCreate() 中设置它,并且在 onSaveInstanceState(Bundle) 方法中,您可以将其保存到当前选项卡 (currentTab = getCurrentTab()),然后在 onRestoreInstanceState() 中你可以重新设置它。
这有道理吗?
请记住,我尚未对此进行测试,但如果您不熟悉这两个方法调用,我愿意这样做。
下面是将数据保存到 Bundle 的示例 - 还记得 onCreate 接受该活动包作为参数。
Just so that I am clear, are you saying that you have several tabs, which are using different orientations (portrait or landscape), and you are having issues when switching tabs and setting the corresponding orientations properly?
In response to Cata's comment:
Yes, so anytime you rotate the screen the currently viewable activity is destroyed and onCreate is called again (if I recall the steps). What you have to do is call getCurrentTab(), which returns the int value representing the tab, and resetting that as the active tab when onCreate is called. You can do this in several ways...either by having a small method that handles just that and calling it via onCreate, or by using onSaveInstanceState(Bundle) to save your current data, and onRestoreInstanceState() to reload your tab data.
You can set a global int (int currentTab = 0), not setting it in onCreate(), and in your onSaveInstanceState(Bundle) method you can save that to the current tab (currentTab = getCurrentTab()), then in onRestoreInstanceState() you can set it again.
Does that make sense?
Keep in mind that I have not tested that, but willing to do so if you aren't familiar with those two method calls.
Below is an example of saving data to the Bundle - also recall that onCreate accepts that activity bundle as a parameter.
我认为最好的解决方案是采用不同的方法,使用单个
Activity
并将每个选项卡内容放在不同的LinearLayout
和onClick()
中> 选项卡(将是一些按钮)在布局之间切换的方法...如果有人有更好的想法,请在此处发布!
谢谢你!
I think the best solution is to take a different approach by using a single
Activity
and put every tab content in a differentLinearLayout
and inonClick()
methods of the tabs(which will be some buttons) to switch between the layouts...If anybody have a better idea, please post it here!
Thank you!