Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stay on current tab after orientation change Actionbar Android

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...

like image 375
Luc Avatar asked Nov 21 '12 10:11

Luc


1 Answers

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);
    }
}
like image 146
Neoh Avatar answered Nov 03 '22 20:11

Neoh