1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-22 14:47:29 +00:00

Fix last selected tab on activity recreate

This commit is contained in:
Ammar Githam 2021-03-28 17:42:09 +09:00
parent dc1119c604
commit 5653fcb41f

View File

@ -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);