简体   繁体   English

屏幕旋转时标签更改[Android]

[英]Tab change on screen rotation [Android]

Hi I have Android APP with 4 tabs (let's say tab1, tab2, tab3, tab4). 嗨,我有4个标签的Android APP(让我们说tab1,tab2,tab3,tab4)。 When activity starts default tab is tab1. 当活动开始时,默认选项卡是tab1。 Than I switch to any other tab (tab2, 3 or 4) and change screen orientation and it always resets to default tab (tab1). 我切换到任何其他选项卡(tab2,3或4)并更改屏幕方向,它总是重置为默认选项卡(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: 在createView()里面我有:

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: CurentTab是private int,默认设置为0,但它在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... 简而言之:我希望Tab在屏幕旋转时不会更改为默认值...

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. 要解决此问题,您可以覆盖选项卡活动的onRetainNonConfigurationInstance()并返回当前选定的选项卡。 In the on onCreate of the tab activity you then call getLastNonConfigurationInstance() which returns the object you returned in onRetainNonConfigurationInstance(). 在选项卡活动的on onCreate中,然后调用getLastNonConfigurationInstance() ,它返回您在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. 如果对象为null,则表示没有方向更改,因此您只需选择第一个选项卡,如果它不为null,则表示屏幕旋转,您可以使用返回的值来确定之前选择的选项卡并设置它再次。

To expand on Flo's answer - Activity method onRetainNonConfigurationInstance() has been deprecated since Honeycomb 3.2 (API 13): 要扩展Flo的答案 - 自Honeycomb 3.2(API 13)以来,不推荐使用Activity方法onRetainNonConfigurationInstance() ):

This method was deprecated in API level 13. 此方法在API级别13中已弃用。
Use the new Fragment API setRetainInstance(boolean) instead; 使用新的Fragment API setRetainInstance(boolean)代替; this is also available on older platforms through the Android compatibility package. 这也可以通过Android兼容包在旧版平台上使用。

Makes usage a little easier, IMHO. 恕我直言,使用更容易一些。

so you have to implement this by overriding onSaveInstanceState(Bundle) in your activity 因此,您必须通过覆盖活动中的onSaveInstanceState(Bundle)来实现此目的

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"));
}

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

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