Handling multiple tabs during landscape and portrait in android

梦想的初衷 提交于 2019-12-06 01:46:59

You can use onRetainNonConfigurationInstance() to solve this issue.

public void onCreate(Bundle savedInstanceState)
{
   ....
   lastTab = (Integer) getLastNonConfigurationInstance();
   .....
   if(lastTab != null)
   {
      tabs.setCurrentTab(lastTab);
   }
}

public Object onRetainNonConfigurationInstance() 
{
   return tabs.getCurrentTab();
}

Rotating the device will, by default, destroy and recreate your Activity. You need to save the state of your selected tab, and restore it when the new Activity is launched.

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    // onCreate implementation goes here

    if(savedInstanceState != null) {
        int selectedTabIndex = savedInstanceState.getInt("selectedTabIndex");
        getActionBar().setSelectedNavigationItem(selectedTabIndex);
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {

    super.onSaveInstanceState(outState);
    outState.putInt("selectedTabIndex", getActionBar().getSelectedNavigationIndex());
}

when you change the orientation it will reload the Activity. That is why its giving 1 st tab.Use in your manifest file android:configChanges="keyboardHidden|orientation" if it will not work fine then go for @Override public void onSaveInstanceState(Bundle savedInstanceState) {}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!