屏幕旋转时选项卡更改 [Android]
您好,我有带有 4 个选项卡的 Android 应用程序(假设 tab1、tab2、tab3、tab4)。当活动启动时,默认选项卡是 tab1。当我切换到任何其他选项卡(选项卡 2、选项卡 3 或选项卡 4)并更改屏幕方向时,它总是重置为默认选项卡(选项卡 1)。
我尝试使用以下代码:
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
setContentView(R.layout.main);
createView();
}
在 createView() 内部我有:
private void createView()
{
... // Tabs are created before
tabHost.getTabWidget().setCurrentTab(CurrentTab);
}
CurentTab
是私有 int 并且默认设置为 0,但它是在 TabChange 上设置的:
public void onTabChanged(String tabId) {
... some code
CurrentTab = tabHost.getCurrentTab();
}
我堆积在这里...有没有还有其他方法来解决这个问题吗?
简而言之:我希望选项卡在屏幕旋转时不会更改为默认值...
Hi I have Android APP with 4 tabs (let's say tab1, tab2, tab3, tab4). When activity starts default tab is tab1. Than I switch to any other tab (tab2, 3 or 4) and change screen orientation and it always resets to default tab (tab1).
I tried with the following code:
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
setContentView(R.layout.main);
createView();
}
And inside createView() I have:
private void createView()
{
... // Tabs are created before
tabHost.getTabWidget().setCurrentTab(CurrentTab);
}
CurentTab
is private int and is default set to 0, but it is set on TabChange:
public void onTabChanged(String tabId) {
... some code
CurrentTab = tabHost.getCurrentTab();
}
I am stacked here... is there any other way to solve this problem?
Shortly: I want that Tab isn't changed to default on screen rotation...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是,在配置更改(例如屏幕旋转)时,当前活动会被破坏并重新创建。对于选项卡活动,这包括选项卡活动本身以及每个选项卡的活动。
因此,当重新创建它时,它只显示第一个选项卡,因为它没有其他信息。
要解决此问题,您可以覆盖
onRetainNonConfigurationInstance()< /code>
您的选项卡活动并返回当前选定的选项卡。然后在选项卡活动的 onCreate 中调用
getLastNonConfigurationInstance()
返回您在 onRetainNonConfigurationInstance() 中返回的对象。如果对象为空,则您知道没有方向变化,因此您只需选择第一个选项卡,如果它不为空,则屏幕旋转,您可以使用返回的值来决定之前选择了哪个选项卡并设置再来一次。The problem is, that on a configuration change like the rotation of the screen the current activity gets destroyed and recreated. In case of a tab activity this includes the tab activity itself and also the activities of each tab.
So when it got recreated it simply shows the first tab as it has no other information.
To fix this you can override
onRetainNonConfigurationInstance()
of you tab activity and return the current selected tab. In the on onCreate of the tab activity you then callgetLastNonConfigurationInstance()
which returns the object you returned in onRetainNonConfigurationInstance(). If the object is null, you know that there was no orientation change so you simply select the first tab, if it isn't null then there was a screen rotation and you can use the returned value to decide which tab was selected before and set it again.因此,您必须通过在活动中重写 onSaveInstanceState(Bundle) 来实现此目的
,因为重新创建屏幕旋转活动时
编辑:
so you have to implement this by overriding onSaveInstanceState(Bundle) in your activity
coz when screen rotate activity is recreated
EDIT:
扩展 Flo 的答案 - Activity 方法
onRetainNonConfigurationInstance()
自 Honeycomb 3.2 (API 13) 起已被弃用:恕我直言,使使用更容易一些。
To expand on Flo's answer - Activity method
onRetainNonConfigurationInstance()
has been deprecated since Honeycomb 3.2 (API 13):Makes usage a little easier, IMHO.