Reputation: 1651
I have specified different layouts for landscape and portrait using layout and layout-land, my application have multiple tabs. Each time when changing from portrait to landscape or landscape to portrait screen changes to 1st tab even selected tab is different one. How can we solve this problem.
Upvotes: 3
Views: 2147
Reputation: 249
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();
}
Upvotes: 1
Reputation: 31283
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());
}
Upvotes: 2
Reputation: 50
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) {}
Upvotes: 0