Save fetched trending searches during configuration changes in TrendingActivity.

This commit is contained in:
Alex Ning 2021-07-18 23:13:28 +08:00
parent 03d8cf497d
commit 78c8452ae4

View File

@ -59,6 +59,8 @@ import retrofit2.Retrofit;
public class TrendingActivity extends BaseActivity { public class TrendingActivity extends BaseActivity {
private static final String TRENDING_SEARCHES_STATE = "TSS";
@BindView(R.id.coordinator_layout_trending_activity) @BindView(R.id.coordinator_layout_trending_activity)
CoordinatorLayout coordinatorLayout; CoordinatorLayout coordinatorLayout;
@BindView(R.id.collapsing_toolbar_layout_trending_activity) @BindView(R.id.collapsing_toolbar_layout_trending_activity)
@ -170,7 +172,14 @@ public class TrendingActivity extends BaseActivity {
swipeRefreshLayout.setEnabled(mSharedPreferences.getBoolean(SharedPreferencesUtils.PULL_TO_REFRESH, true)); swipeRefreshLayout.setEnabled(mSharedPreferences.getBoolean(SharedPreferencesUtils.PULL_TO_REFRESH, true));
swipeRefreshLayout.setOnRefreshListener(this::loadTrendingSearches); swipeRefreshLayout.setOnRefreshListener(this::loadTrendingSearches);
loadTrendingSearches(); if (savedInstanceState != null) {
trendingSearches = savedInstanceState.getParcelableArrayList(TRENDING_SEARCHES_STATE);
}
if (trendingSearches != null) {
adapter.setTrendingSearches(trendingSearches);
} else {
loadTrendingSearches();
}
} }
private void loadTrendingSearches() { private void loadTrendingSearches() {
@ -246,6 +255,12 @@ public class TrendingActivity extends BaseActivity {
errorTextView.setText(stringId); errorTextView.setText(stringId);
} }
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelableArrayList(TRENDING_SEARCHES_STATE, trendingSearches);
}
@Override @Override
protected SharedPreferences getDefaultSharedPreferences() { protected SharedPreferences getDefaultSharedPreferences() {
return mSharedPreferences; return mSharedPreferences;