屏幕旋转时选项卡更改 [Android]

发布于 2024-11-01 04:24:15 字数 813 浏览 5 评论 0原文

您好,我有带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沧桑㈠ 2024-11-08 04:24:15

问题是,在配置更改(例如屏幕旋转)时,当前活动会被破坏并重新创建。对于选项卡活动,这包括选项卡活动本身以及每个选项卡的活动。

因此,当重新创建它时,它只显示第一个选项卡,因为它没有其他信息。

要解决此问题,您可以覆盖 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 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.

难忘№最初的完美 2024-11-08 04:24:15

因此,您必须通过在活动中重写 onSaveInstanceState(Bundle) 来实现此目的

,因为重新创建屏幕旋转活动时

编辑:

protected void onSaveInstanceState (Bundle outState){
    outState.putInt("LastTab", tabHost.getCurrentTab());
}


protected void onCreate (Bundle savedInstanceState){
  super.onCreate (savedInstanceState);
  //...tabs creation
  // u need to provide some code to check if "LastTab" exists in savedState
  tabHost.getTabWidget().setCurrentTab(savedInstanceState.getInt("LastTab"));
}

so you have to implement this by overriding onSaveInstanceState(Bundle) in your activity

coz when screen rotate activity is recreated

EDIT:

protected void onSaveInstanceState (Bundle outState){
    outState.putInt("LastTab", tabHost.getCurrentTab());
}


protected void onCreate (Bundle savedInstanceState){
  super.onCreate (savedInstanceState);
  //...tabs creation
  // u need to provide some code to check if "LastTab" exists in savedState
  tabHost.getTabWidget().setCurrentTab(savedInstanceState.getInt("LastTab"));
}
冰雪梦之恋 2024-11-08 04:24:15

扩展 Flo 的答案 - Activity 方法onRetainNonConfigurationInstance() 自 Honeycomb 3.2 (API 13) 起已被弃用:

此方法在 API 级别 13 中已弃用。
使用新的 Fragment API setRetainInstance(boolean) 相反;这也可以通过 Android 兼容包在旧平台上使用。

恕我直言,使使用更容易一些。

To expand on Flo's answer - Activity method onRetainNonConfigurationInstance() has been deprecated since Honeycomb 3.2 (API 13):

This method was deprecated in API level 13.
Use the new Fragment API setRetainInstance(boolean) instead; this is also available on older platforms through the Android compatibility package.

Makes usage a little easier, IMHO.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文