CP-Remember the last tab
This should reember the last tab
This commit is contained in:
parent
9de67b59a4
commit
305f761705
@ -1,9 +1,12 @@
|
|||||||
package com.philliphsu.clock2;
|
package com.philliphsu.clock2;
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.PersistableBundle;
|
||||||
|
import android.preference.PreferenceManager;
|
||||||
import android.support.design.widget.FloatingActionButton;
|
import android.support.design.widget.FloatingActionButton;
|
||||||
import android.support.design.widget.TabLayout;
|
import android.support.design.widget.TabLayout;
|
||||||
import android.support.v4.app.Fragment;
|
import android.support.v4.app.Fragment;
|
||||||
@ -34,6 +37,8 @@ public class MainActivity extends BaseActivity {
|
|||||||
public static final String EXTRA_SHOW_PAGE = "com.philliphsu.clock2.extra.SHOW_PAGE";
|
public static final String EXTRA_SHOW_PAGE = "com.philliphsu.clock2.extra.SHOW_PAGE";
|
||||||
|
|
||||||
public static final int REQUEST_THEME_CHANGE = 5;
|
public static final int REQUEST_THEME_CHANGE = 5;
|
||||||
|
private static final String LAST_TAB = "last_tab";
|
||||||
|
private static final int DEFAULT_TAB = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The {@link android.support.v4.view.PagerAdapter} that will provide
|
* The {@link android.support.v4.view.PagerAdapter} that will provide
|
||||||
@ -244,6 +249,15 @@ public class MainActivity extends BaseActivity {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
final String key = getString(R.string.key_last_tab);
|
||||||
|
final int fromPrefsLastTab = PreferenceManager.getDefaultSharedPreferences(this).getInt(key, DEFAULT_TAB);
|
||||||
|
restoreTab(savedInstanceState, fromPrefsLastTab);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void restoreTab(final Bundle savedInstanceState, int def) {
|
||||||
|
final int tabToSwitchTo = savedInstanceState == null ?
|
||||||
|
def : savedInstanceState.getInt(LAST_TAB, def);
|
||||||
|
mViewPager.setCurrentItem(tabToSwitchTo);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -293,6 +307,30 @@ public class MainActivity extends BaseActivity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSaveInstanceState(final Bundle outState, final PersistableBundle outPersistentState) {
|
||||||
|
super.onSaveInstanceState(outState, outPersistentState);
|
||||||
|
outState.putInt(LAST_TAB, mViewPager.getCurrentItem());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onRestoreInstanceState(final Bundle savedInstanceState) {
|
||||||
|
super.onRestoreInstanceState(savedInstanceState);
|
||||||
|
restoreTab(savedInstanceState, DEFAULT_TAB);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressLint("CommitPrefEdits")
|
||||||
|
@Override
|
||||||
|
protected void onDestroy() {
|
||||||
|
final int currentTab = mViewPager.getCurrentItem();
|
||||||
|
PreferenceManager.getDefaultSharedPreferences(this)
|
||||||
|
.edit()
|
||||||
|
.putInt(getString(R.string.key_last_tab), currentTab)
|
||||||
|
.commit();
|
||||||
|
|
||||||
|
super.onDestroy();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected int layoutResId() {
|
protected int layoutResId() {
|
||||||
return R.layout.activity_main;
|
return R.layout.activity_main;
|
||||||
|
|||||||
@ -12,4 +12,6 @@
|
|||||||
<item>@string/theme_light</item>
|
<item>@string/theme_light</item>
|
||||||
<item>@string/theme_dark</item>
|
<item>@string/theme_dark</item>
|
||||||
</string-array>
|
</string-array>
|
||||||
|
|
||||||
|
<string name="key_last_tab">last_tab</string>
|
||||||
</resources>
|
</resources>
|
||||||
Loading…
Reference in New Issue
Block a user