Luc
Luc

Reputation: 627

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

Upvotes: 5

Views: 4252

Answers (3)

JoachimR
JoachimR

Reputation: 5258

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);
        }
    }

Upvotes: 0

Neoh
Neoh

Reputation: 16174

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);
    }
}

Upvotes: 25

Till - gotohuman.com
Till - gotohuman.com

Reputation: 6075

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);
}

Upvotes: -1

Related Questions