Remembering users previous tab selection on Android

// Inside your main MainActivity Integer tabPref; SharedPreferences prefs; // Inside onCreate method in your MainActivity prefs = getPreferences(Context.MODE_PRIVATE); tabPref = prefs.getInt("tabPref", 0); // You need to make sure that when each tab is selected, the preference is also set, and editor.apply() called mViewPager .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { @Override public void onPageSelected(int position) { actionBar.setSelectedNavigationItem(position); SharedPreferences.Editor editor = prefs.edit().putInt( "tabPref", position); editor.apply(); } }); // Finally, at the end of onCreate method, // you need to make sure that the tab determined in the preferences is selected with mViewPager.setCurrentItem(tabPref, false);
This snippets shows how to make your app remember users previous tab selection on Android.

#android #tab #selection #ViewPager

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.