Handle multiple tabs during landscape and portrait in android
I have specified different layouts for landscape and portrait using layout and layout, my application has multiple tabs. Every time you change from portrait to landscape or landscape to portrait, the screen changes to 1st tab, even the selected tab is different. How can we solve this problem.
+3
source to share
3 answers
You can use onRetainNonConfigurationInstance () to solve this problem.
public void onCreate(Bundle savedInstanceState)
{
....
lastTab = (Integer) getLastNonConfigurationInstance();
.....
if(lastTab != null)
{
tabs.setCurrentTab(lastTab);
}
}
public Object onRetainNonConfigurationInstance()
{
return tabs.getCurrentTab();
}
+1
source to share
Rotating the device by default will destroy and recreate your activity. You need to save the state of the selected tab and restore it when starting a new activity.
@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());
}
+2
source to share