I have 2 tabs, for example Tab1 and Tab2, which are displayed on the screen. Let the tabs appear in the PORTRAIT orientation.
Tab1 displays Activity1 and Tab2 displays Activity2.
The currently selected tab state is Tab2. Now I am changing the orientation for the PORTRAIT in the LANDSCAPE. When changing the orientation in LANDSCAPE mode, the Tab1 tab is displayed instead of displaying Tab2.
Basically, I want to keep the Tab state when changing orientation.
To complete the task of saving the state of a tab, I write the following code:
protected void onPause() { super.onPause(); saveCurrentTabState(getSelectedTab()); } private void saveCurrentTabState(int value) { PreferenceManager.getDefaultSharedPreferences(this).edit().putInt( "tabState", value).commit(); } @Override protected void onResume() { super.onResume(); setCurrentTab(PreferenceManager.getDefaultSharedPreferences(this) .getInt("tabState", 0)); }
I wanted to know whether my approach is correct or not, and whether the above code matches the correct way to maintain the tab state when changing orientation.
android
chiranjib
source share