I have a tablayout associated with a viewpager. Everytime I pull to refresh, currently I try to reload the entire page (calling finish and starting the activity again).Say I have 4 items in my tab layout (dynamic) , I would like to have it such that, everytime I refresh, I can save the state of the item selected prior to refresh and then display it after the entire screen is reloaded.
I tried a couple of things recommended online but had no luck.
Here's the code I can potentially use, but not sure how to go about it :
void selectPage(int pageIndex)
{
mViewPager.setCurrentItem(pageIndex);
mTabLayout.setupWithViewPager(mViewPager);
}
Also,
@BindView(R.id.lists_view_pager)
ViewPager mViewPager;
@BindView(R.id.lists_refresh)
SwipeRefreshLayout mRefresh;
@BindView(R.id.tab_layout)
TabLayout mTabLayout;
On create I call:
mRefresh.setOnRefreshListener(this::refreshassistant);
refreshassistant is as follows :
public void refreshassistant(){
finish();
startActivity(getIntent());
mPagerAdapter.notifyDataSetChanged();
overridePendingTransition(0, 0);
mPresenter.onRefresh();
}
I would like something like if I select tab 2, it shows content of tab 2 and then when I refresh/reload the entire page, i am able to get back tab 2 and its contents. Any idea how to go about it?
Thanks in advance!
You can use following two methods to store and retrieve your values from your pages:
public static void setDefaults(String key, String value, Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(key, value);
editor.commit();
}
public static String getDefaults(String key, Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(key, null); //if no value found returns null
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.