在屏幕旋转选项卡中更改[机器人] [英] Tab change on screen rotation [Android]

查看:166
本文介绍了在屏幕旋转选项卡中更改[机器人]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有Android应用程序有4个选项卡(比方说TAB1,TAB2,TAB3,TAB4)。当活动开始默认选项卡是TAB1。比我切换到任何其他选项卡(TAB2,3或4),并更改屏幕显示方向,它总是重置为默认选项卡(TAB1)。

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).

我试着用下面的code:

I tried with the following code:

@Override
public void onConfigurationChanged(Configuration newConfig)
{
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.main);
    createView();
}

和内CreateView的()我有:

And inside createView() I have:

private void createView()
{
... // Tabs are created before
tabHost.getTabWidget().setCurrentTab(CurrentTab);
}

CurentTab 是私人诠释,并且默认设置为0,但它被设置在TabChange:

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...

推荐答案

的问题是,像屏幕上的当前活动被破坏并重新创建的转动配置更改。万一一个标签活性这包括的标签活动本身和每个选项卡的也活动。

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.

要解决这个问题,你可以覆盖<一个href="http://developer.android.com/reference/android/app/Activity.html#onRetainNonConfigurationInstance%28%29"相对=nofollow> onRetainNonConfigurationInstance() 您标签的活动,并返回当前选中的标签。在该选项卡活动上的onCreate你再调用<一个href="http://developer.android.com/reference/android/app/Activity.html#getLastNonConfigurationInstance%28%29"相对=nofollow> getLastNonConfigurationInstance() 返回你onRetainNonConfigurationInstance返回的对象()。如果对象是空的,你知道,没有方向的变化,所以你只需选择第一个选项卡,如果它不为空,然后有一个屏幕旋转,您可以使用返回值来决定哪个选项卡被选中之前,设置它一次。

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 call getLastNonConfigurationInstance() 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.

这篇关于在屏幕旋转选项卡中更改[机器人]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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