简体   繁体   English

TabActivity始终在方向更改时加载第一个标签

[英]TabActivity Always loading first tab on orientation change

A tabactivity has 3 tabs in it. 一个制表活动中有3个选项卡。 in onCreate() after adding tabs programatically, there is a call to tabHost.setCurrentTab(currentTab);setDefaultTab(currentTab); 在以编程方式添加选项卡之后的onCreate()中,有一个对tabHost.setCurrentTab(currentTab);setDefaultTab(currentTab);的调用tabHost.setCurrentTab(currentTab);setDefaultTab(currentTab); To display a specific tab. 显示特定标签。 However, This statement has no effect when orientation is changed and onCreate() is called. 但是,当更改方向并调用onCreate()时,此语句无效。 The tabactivity always shows first tab. 制表活动始终显示第一个选项卡。 How to set a specific tab on orientation change? 如何在方向更改上设置特定的标签?

Regards 问候

Simply call setCurrentTab(x) in onCreate(Bundle savedInstanceState) where x is the last selected tab. 只需在onCreate(Bundle savedInstanceState)中调用setCurrentTab(x) ,其中x是最后选择的选项卡。 When device changes orientation activities are recreated. 设备更改时,将重新创建方向活动。 So when activity is about to be destroyed you have to save its state and restore it when it's recreated. 因此,当活动即将被销毁时,您必须保存其状态并在重新创建活动时将其还原。 onSaveInstanceState (Bundle outState) is where you save the state. onSaveInstanceState(Bundle outState)是保存状态的位置。 Override this method and put currently selected tab to outState. 覆盖此方法,并将当前选择的选项卡置于outState。 Then in onCreate (Bundle savedInstanceState) you get currently selected tab from savedInstanceState . 然后在onCreate(捆绑的savedInstanceState)中,您从savedInstanceState获得当前选择的选项卡。 Actually outState = savedInstanceState . 实际上outState = savedInstanceState Basically this how it's done. 基本上,这是怎么做的。

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

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