Browse Source

Remove usage of final arrays to hold values

renovate/org.robolectric-robolectric-4.x
Ammar Githam 4 years ago
parent
commit
951117c769
  1. 16
      app/src/main/java/awais/instagrabber/utils/NavigationExtensions.java

16
app/src/main/java/awais/instagrabber/utils/NavigationExtensions.java

@ -24,6 +24,10 @@ import awais.instagrabber.R;
* from architecture-components-samples. Some modifications have been done, check git history. * from architecture-components-samples. Some modifications have been done, check git history.
*/ */
public class NavigationExtensions { public class NavigationExtensions {
private static String selectedItemTag;
private static boolean isOnFirstFragment;
@NonNull @NonNull
public static LiveData<NavController> setupWithNavController(@NonNull final BottomNavigationView bottomNavigationView, public static LiveData<NavController> setupWithNavController(@NonNull final BottomNavigationView bottomNavigationView,
@NonNull List<Integer> navGraphIds, @NonNull List<Integer> navGraphIds,
@ -51,15 +55,15 @@ public class NavigationExtensions {
detachNavHostFragment(fragmentManager, navHostFragment); detachNavHostFragment(fragmentManager, navHostFragment);
} }
} }
final String[] selectedItemTag = {graphIdToTagMap.get(bottomNavigationView.getSelectedItemId())};
selectedItemTag = graphIdToTagMap.get(bottomNavigationView.getSelectedItemId());
final String firstFragmentTag = graphIdToTagMap.get(firstFragmentGraphId); final String firstFragmentTag = graphIdToTagMap.get(firstFragmentGraphId);
final boolean[] isOnFirstFragment = {selectedItemTag[0] != null && selectedItemTag[0].equals(firstFragmentTag)};
isOnFirstFragment = selectedItemTag != null && selectedItemTag.equals(firstFragmentTag);
bottomNavigationView.setOnNavigationItemSelectedListener(item -> { bottomNavigationView.setOnNavigationItemSelectedListener(item -> {
if (fragmentManager.isStateSaved()) { if (fragmentManager.isStateSaved()) {
return false; return false;
} }
String newlySelectedItemTag = graphIdToTagMap.get(item.getItemId()); String newlySelectedItemTag = graphIdToTagMap.get(item.getItemId());
String tag = selectedItemTag[0];
String tag = selectedItemTag;
if (tag != null && !tag.equals(newlySelectedItemTag)) { if (tag != null && !tag.equals(newlySelectedItemTag)) {
fragmentManager.popBackStack(firstFragmentTag, FragmentManager.POP_BACK_STACK_INCLUSIVE); fragmentManager.popBackStack(firstFragmentTag, FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fragment fragment = fragmentManager.findFragmentByTag(newlySelectedItemTag); Fragment fragment = fragmentManager.findFragmentByTag(newlySelectedItemTag);
@ -94,8 +98,8 @@ public class NavigationExtensions {
.setReorderingAllowed(true) .setReorderingAllowed(true)
.commit(); .commit();
} }
selectedItemTag[0] = newlySelectedItemTag;
isOnFirstFragment[0] = selectedItemTag[0].equals(firstFragmentTag);
selectedItemTag = newlySelectedItemTag;
isOnFirstFragment = selectedItemTag.equals(firstFragmentTag);
selectedNavController.setValue(selectedFragment.getNavController()); selectedNavController.setValue(selectedFragment.getNavController());
return true; return true;
} }
@ -105,7 +109,7 @@ public class NavigationExtensions {
setupDeepLinks(bottomNavigationView, navGraphIds, fragmentManager, containerId, intent); setupDeepLinks(bottomNavigationView, navGraphIds, fragmentManager, containerId, intent);
final int finalFirstFragmentGraphId = firstFragmentGraphId; final int finalFirstFragmentGraphId = firstFragmentGraphId;
fragmentManager.addOnBackStackChangedListener(() -> { fragmentManager.addOnBackStackChangedListener(() -> {
if (!isOnFirstFragment[0]) {
if (!isOnFirstFragment) {
if (firstFragmentTag == null) { if (firstFragmentTag == null) {
return; return;
} }

Loading…
Cancel
Save