Saving tabLayout position when Back button pressed
<p>how can I save and return the tab position when user press the Back button?</p>

<pre><code>@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    ViewPager vpPager = (ViewPager) findViewById(R.id.viewpager);
    adapterViewPager = new MyPagerAdapter(getSupportFragmentManager());
    vpPager.setAdapter(adapterViewPager);

    // Give the TabLayout the ViewPager
    tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    tabLayout.setTabMode(TabLayout.MODE_FIXED);
    tabLayout.setupWithViewPager(vpPager);

    // Attach the page change listener inside the activity
    vpPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt(POSITION, tabLayout.getSelectedTabPosition());
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    viewPager.setCurrentItem(savedInstanceState.getInt(POSITION));
}
</code></pre>

<p>Do I use the onBackPressed() to do this?
Help is much appreciated. Thanks!</p>