From 444c51a36d7626c54cf793cd7381b15edd9d6a41 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Sat, 19 Sep 2020 18:48:58 +0900 Subject: [PATCH] Reselect proper tab immediately after logging off --- .../instagrabber/activities/MainActivity.java | 51 ++++++++++++------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/activities/MainActivity.java b/app/src/main/java/awais/instagrabber/activities/MainActivity.java index ef176505..a5106baf 100644 --- a/app/src/main/java/awais/instagrabber/activities/MainActivity.java +++ b/app/src/main/java/awais/instagrabber/activities/MainActivity.java @@ -369,32 +369,26 @@ public class MainActivity extends BaseLanguageActivity { final boolean isLoggedIn = !TextUtils.isEmpty(cookie) && CookieUtils.getUserIdFromCookie(cookie) != null; if (!isLoggedIn) { main_nav_ids = R.array.logged_out_main_nav_ids; + final int selectedItemId = binding.bottomNavView.getSelectedItemId(); binding.bottomNavView.getMenu().clear(); binding.bottomNavView.inflateMenu(R.menu.logged_out_bottom_navigation_menu); + if (selectedItemId == R.id.profile_nav_graph + || selectedItemId == R.id.more_nav_graph) { + binding.bottomNavView.setSelectedItemId(selectedItemId); + } else { + setBottomNavSelectedItem(R.navigation.profile_nav_graph); + } } - final TypedArray navIds = getResources().obtainTypedArray(main_nav_ids); - final List mainNavList = new ArrayList<>(navIds.length()); - final int length = navIds.length(); - for (int i = 0; i < length; i++) { - final int resourceId = navIds.getResourceId(i, -1); - if (resourceId < 0) continue; - mainNavList.add(resourceId); - } - navIds.recycle(); - if (setDefaultFromSettings || !isLoggedIn) { + final List mainNavList = getMainNavList(main_nav_ids); + if (setDefaultFromSettings) { final String defaultTabIdString = settingsHelper.getString(Constants.DEFAULT_TAB); try { - final int defaultNavId = TextUtils.isEmpty(defaultTabIdString) || !isLoggedIn + final int defaultNavId = TextUtils.isEmpty(defaultTabIdString) ? R.navigation.profile_nav_graph : Integer.parseInt(defaultTabIdString); final int index = mainNavList.indexOf(defaultNavId); - if (index >= 0) { - firstFragmentGraphIndex = index; - final Integer menuId = NAV_TO_MENU_ID_MAP.get(defaultNavId); - if (menuId != null) { - binding.bottomNavView.setSelectedItemId(menuId); - } - } + if (index >= 0) firstFragmentGraphIndex = index; + setBottomNavSelectedItem(defaultNavId); } catch (NumberFormatException e) { Log.e(TAG, "Error parsing id", e); } @@ -410,6 +404,27 @@ public class MainActivity extends BaseLanguageActivity { currentNavControllerLiveData = navControllerLiveData; } + private void setBottomNavSelectedItem(final int navId) { + final Integer menuId = NAV_TO_MENU_ID_MAP.get(navId); + if (menuId != null) { + binding.bottomNavView.setSelectedItemId(menuId); + } + } + + @NonNull + private List getMainNavList(final int main_nav_ids) { + final TypedArray navIds = getResources().obtainTypedArray(main_nav_ids); + final List mainNavList = new ArrayList<>(navIds.length()); + final int length = navIds.length(); + for (int i = 0; i < length; i++) { + final int resourceId = navIds.getResourceId(i, -1); + if (resourceId < 0) continue; + mainNavList.add(resourceId); + } + navIds.recycle(); + return mainNavList; + } + private void setupNavigation(final NavController navController) { NavigationUI.setupWithNavController(binding.toolbar, navController); navController.addOnDestinationChangedListener((controller, destination, arguments) -> {