From 4ecf9ca605d027eaeb13b98849d7ac66c8d4fd16 Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Wed, 25 Mar 2020 15:32:26 +0100 Subject: [PATCH] Fixed sidebar sometimes highlighting wrong fragment --- .../antennapod/activity/MainActivity.java | 4 +++- .../fragment/NavDrawerFragment.java | 19 +++++++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java index 6dab5ab45..872174403 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java @@ -176,6 +176,7 @@ public class MainActivity extends CastEnabledActivity { default: // default to the queue fragment = new QueueFragment(); + tag = QueueFragment.TAG; args = null; break; } @@ -183,6 +184,7 @@ public class MainActivity extends CastEnabledActivity { if (args != null) { fragment.setArguments(args); } + NavDrawerFragment.saveLastNavFragment(this, tag); loadFragment(fragment); } @@ -191,6 +193,7 @@ public class MainActivity extends CastEnabledActivity { if (args != null) { fragment.setArguments(args); } + NavDrawerFragment.saveLastNavFragment(this, String.valueOf(feedId)); loadFragment(fragment); } @@ -367,7 +370,6 @@ public class MainActivity extends CastEnabledActivity { super.onBackPressed(); } else { loadFragment(UserPreferences.getBackButtonGoToPage(), null); - NavDrawerFragment.saveLastNavFragment(this, UserPreferences.getBackButtonGoToPage()); } break; default: super.onBackPressed(); diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/NavDrawerFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/NavDrawerFragment.java index 63969345c..0dff8f24b 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/NavDrawerFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/NavDrawerFragment.java @@ -48,7 +48,7 @@ import org.greenrobot.eventbus.Subscribe; import java.util.List; public class NavDrawerFragment extends Fragment implements AdapterView.OnItemClickListener, - AdapterView.OnItemLongClickListener { + AdapterView.OnItemLongClickListener, SharedPreferences.OnSharedPreferenceChangeListener { @VisibleForTesting public static final String PREF_LAST_FRAGMENT_TAG = "prefLastFragmentTag"; @VisibleForTesting @@ -88,6 +88,8 @@ public class NavDrawerFragment extends Fragment implements AdapterView.OnItemCli root.findViewById(R.id.nav_settings).setOnClickListener(v -> { startActivity(new Intent(getActivity(), PreferenceActivity.class)); }); + getContext().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE) + .registerOnSharedPreferenceChangeListener(this); return root; } @@ -124,6 +126,8 @@ public class NavDrawerFragment extends Fragment implements AdapterView.OnItemCli if (disposable != null) { disposable.dispose(); } + getContext().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE) + .unregisterOnSharedPreferenceChangeListener(this); } @Override @@ -192,7 +196,6 @@ public class NavDrawerFragment extends Fragment implements AdapterView.OnItemCli } else { showMainActivity(EpisodesFragment.TAG); } - saveLastNavFragment(getContext(), EpisodesFragment.TAG); } } }; @@ -371,7 +374,6 @@ public class NavDrawerFragment extends Fragment implements AdapterView.OnItemCli } else { showMainActivity(tag); } - saveLastNavFragment(getContext(), tag); } else { int pos = position - navAdapter.getSubscriptionOffset(); long feedId = navDrawerData.feeds.get(pos).getId(); @@ -382,10 +384,7 @@ public class NavDrawerFragment extends Fragment implements AdapterView.OnItemCli intent.putExtra(MainActivity.EXTRA_FEED_ID, feedId); startActivity(intent); } - saveLastNavFragment(getContext(), String.valueOf(feedId)); } - selectedNavListIndex = position; - navAdapter.notifyDataSetChanged(); } } @@ -418,4 +417,12 @@ public class NavDrawerFragment extends Fragment implements AdapterView.OnItemCli Log.d(TAG, "getLastNavFragment() -> " + lastFragment); return lastFragment; } + + @Override + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { + if (PREF_LAST_FRAGMENT_TAG.equals(key)) { + updateSelection(); + navAdapter.notifyDataSetChanged(); + } + } }