In my application the selected tab in the actionbar is set to the first one when the orientation is changed, i'd like it to stay on the selected tab, and not jump to the first tab in line...
Asked
Active
Viewed 4,222 times
3 Answers
25
You can actually do this very easily using onSavedInstanceState:
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
int i = getActionBar().getSelectedNavigationIndex();
outState.putInt("index", i);
}
Then include this in your onCreate() method:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
if(savedInstanceState != null) {
int index = savedInstanceState.getInt("index");
getActionBar().setSelectedNavigationItem(index);
}
}

Neoh
- 15,906
- 14
- 66
- 78
-
`getSelectedNavigationIndex()` is deprecated at API level 21. What should be the alternative one to this function? – Shnkc Jan 20 '15 at 12:53
0
For 21+ with toolbar this works for me:
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
public class MainActivity extends AppCompatActivity {
private ViewPager viewPager;
private static final String FLAG_CURRENTLY_SELECTED_TAB_INDEX = "currentlySelectedTabIndex";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setHomeAsUpIndicator(R.mipmap.ic_launcher);
actionBar.setDisplayHomeAsUpEnabled(true);
}
viewPager = (ViewPager) findViewById(R.id.viewpager);
FragmentManager fragmentManager = getSupportFragmentManager();
viewPager.setAdapter(getMyCustomAdapter());
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
if (savedInstanceState != null) {
final int currentlySelectedTabIndex = savedInstanceState.getInt(FLAG_CURRENTLY_SELECTED_TAB_INDEX, -1);
if (currentlySelectedTabIndex > -1) {
viewPager.setCurrentItem(currentlySelectedTabIndex);
}
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (viewPager != null) {
int i = viewPager.getCurrentItem();
outState.putInt(FLAG_CURRENTLY_SELECTED_TAB_INDEX, i);
}
}

JoachimR
- 5,150
- 7
- 45
- 50
-1
Since getLastNonConfigurationInstance()
has been deprecated, in your FragmentActivity simply override onRetainCustomNonConfigurationInstance() and use the new getLastCustomNonConfigurationInstance()
:
@Override
public Object onRetainCustomNonConfigurationInstance() {
return mViewPager.getCurrentItem();
}
and retrieve it in your onCreate()
like this:
Integer lastTab = (Integer) getLastCustomNonConfigurationInstance();
if (lastTab != null) {
mViewPager.setCurrentItem(lastTab);
}

Till - Appviewer.io
- 4,529
- 1
- 31
- 35