Browse Source

Fix last selected tab on activity recreate

renovate/org.robolectric-robolectric-4.x
Ammar Githam 4 years ago
parent
commit
5653fcb41f
  1. 14
      app/src/main/java/awais/instagrabber/activities/MainActivity.java

14
app/src/main/java/awais/instagrabber/activities/MainActivity.java

@ -94,6 +94,7 @@ import static awais.instagrabber.utils.Utils.settingsHelper;
public class MainActivity extends BaseLanguageActivity implements FragmentManager.OnBackStackChangedListener { public class MainActivity extends BaseLanguageActivity implements FragmentManager.OnBackStackChangedListener {
private static final String TAG = "MainActivity"; private static final String TAG = "MainActivity";
private static final String FIRST_FRAGMENT_GRAPH_INDEX_KEY = "firstFragmentGraphIndex"; private static final String FIRST_FRAGMENT_GRAPH_INDEX_KEY = "firstFragmentGraphIndex";
private static final String LAST_SELECT_NAV_MENU_ID = "lastSelectedNavMenuId";
private ActivityMainBinding binding; private ActivityMainBinding binding;
private LiveData<NavController> currentNavControllerLiveData; private LiveData<NavController> currentNavControllerLiveData;
@ -105,6 +106,7 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
private boolean showSearch = true; private boolean showSearch = true;
private Handler suggestionsFetchHandler; private Handler suggestionsFetchHandler;
private int firstFragmentGraphIndex; private int firstFragmentGraphIndex;
private int lastSelectedNavMenuId;
private boolean isActivityCheckerServiceBound = false; private boolean isActivityCheckerServiceBound = false;
private boolean isBackStackEmpty = false; private boolean isBackStackEmpty = false;
private boolean isLoggedIn; private boolean isLoggedIn;
@ -201,6 +203,7 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
@Override @Override
protected void onSaveInstanceState(@NonNull final Bundle outState) { protected void onSaveInstanceState(@NonNull final Bundle outState) {
outState.putString(FIRST_FRAGMENT_GRAPH_INDEX_KEY, String.valueOf(firstFragmentGraphIndex)); outState.putString(FIRST_FRAGMENT_GRAPH_INDEX_KEY, String.valueOf(firstFragmentGraphIndex));
outState.putString(LAST_SELECT_NAV_MENU_ID, String.valueOf(binding.bottomNavView.getSelectedItemId()));
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
} }
@ -213,6 +216,12 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
firstFragmentGraphIndex = Integer.parseInt(key); firstFragmentGraphIndex = Integer.parseInt(key);
} catch (NumberFormatException ignored) { } } catch (NumberFormatException ignored) { }
} }
final String lastSelected = (String) savedInstanceState.get(LAST_SELECT_NAV_MENU_ID);
if (lastSelected != null) {
try {
lastSelectedNavMenuId = Integer.parseInt(lastSelected);
} catch (NumberFormatException ignored) { }
}
setupBottomNavigationBar(false); setupBottomNavigationBar(false);
} }
@ -461,7 +470,6 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
private void setupBottomNavigationBar(final boolean setDefaultTabFromSettings) { private void setupBottomNavigationBar(final boolean setDefaultTabFromSettings) {
currentTabs = !isLoggedIn ? setupAnonBottomNav() : setupMainBottomNav(); currentTabs = !isLoggedIn ? setupAnonBottomNav() : setupMainBottomNav();
final List<Integer> mainNavList = currentTabs.stream() final List<Integer> mainNavList = currentTabs.stream()
.map(Tab::getNavigationResId) .map(Tab::getNavigationResId)
.collect(Collectors.toList()); .collect(Collectors.toList());
@ -470,6 +478,8 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
.collect(Collectors.toList()); .collect(Collectors.toList());
if (setDefaultTabFromSettings) { if (setDefaultTabFromSettings) {
setSelectedTab(currentTabs); setSelectedTab(currentTabs);
} else {
binding.bottomNavView.setSelectedItemId(lastSelectedNavMenuId);
} }
final LiveData<NavController> navControllerLiveData = setupWithNavController( final LiveData<NavController> navControllerLiveData = setupWithNavController(
binding.bottomNavView, binding.bottomNavView,
@ -507,7 +517,7 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
return tab.getNavigationResId() == defaultNavId; return tab.getNavigationResId() == defaultNavId;
}); });
if (index < 0 || index >= tabs.size()) index = 0; if (index < 0 || index >= tabs.size()) index = 0;
if (index >= 0) firstFragmentGraphIndex = index;
firstFragmentGraphIndex = index;
setBottomNavSelectedTab(tabs.get(index)); setBottomNavSelectedTab(tabs.get(index));
} catch (Exception e) { } catch (Exception e) {
Log.e(TAG, "Error parsing id", e); Log.e(TAG, "Error parsing id", e);

Loading…
Cancel
Save