MainActivity: Fix memory leaks when navigating between bottom nav destinations

This commit is contained in:
Ifeoluwa Andrew Omole 2025-01-28 13:19:26 +01:00
parent e96ab2bd22
commit c0c4d505ac
4 changed files with 54 additions and 26 deletions

View file

@ -208,6 +208,8 @@ public class MainActivity extends BaseActivity
//showBottom so that we do not show the bottom tray again when constructing
//from the saved instance state.
freeUpFragments();
if (fragment != null && args != null) {
fragment.setArguments(args);
}
@ -261,6 +263,19 @@ public class MainActivity extends BaseActivity
return false;
}
/**
* Old implementation of loadFragment() was causing memory leaks, due to MainActivity holding
* references to cleared fragments. This function frees up all fragment references.
* <p>
* Called in loadFragment() before doing the actual loading.
**/
public void freeUpFragments() {
contributionsFragment = null;
nearbyParentFragment = null;
exploreFragment = null;
bookmarkFragment = null;
}
public void hideTabs() {
binding.fragmentMainNavTabLayout.setVisibility(View.GONE);
}
@ -452,6 +467,7 @@ public class MainActivity extends BaseActivity
bundle.putDouble("prev_longitude", longitude);
loadFragment(ExploreFragment.newInstance(), false, bundle);
setSelectedItemId(NavTab.EXPLORE.code());
}
/**
@ -469,6 +485,7 @@ public class MainActivity extends BaseActivity
bundle.putDouble("prev_longitude", longitude);
loadFragment(NearbyParentFragment.newInstance(), false, bundle);
setSelectedItemId(NavTab.NEARBY.code());
}
@Override

View file

@ -196,33 +196,38 @@ public class ExploreFragment extends CommonsDaggerSupportFragment {
*/
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.explore_fragment_menu, menu);
// if logged in 'Show in Nearby' menu item is visible
if (applicationKvStore.getBoolean("login_skipped") == false) {
inflater.inflate(R.menu.explore_fragment_menu, menu);
MenuItem others = menu.findItem(R.id.list_item_show_in_nearby);
MenuItem others = menu.findItem(R.id.list_item_show_in_nearby);
if (binding.viewPager.getCurrentItem() == 2) {
others.setVisible(true);
}
// if on Map tab, show all menu options, else only show search
binding.viewPager.addOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
if (binding.viewPager.getCurrentItem() == 2) {
others.setVisible(true);
}
@Override
public void onPageSelected(int position) {
others.setVisible((position == 2));
}
@Override
public void onPageScrollStateChanged(int state) {
if (state == SCROLL_STATE_IDLE && binding.viewPager.getCurrentItem() == 2) {
onPageSelected(2);
// if on Map tab, show all menu options, else only show search
binding.viewPager.addOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
}
}
});
@Override
public void onPageSelected(int position) {
others.setVisible((position == 2));
}
@Override
public void onPageScrollStateChanged(int state) {
if (state == SCROLL_STATE_IDLE && binding.viewPager.getCurrentItem() == 2) {
onPageSelected(2);
}
}
});
} else {
inflater.inflate(R.menu.menu_search, menu);
}
super.onCreateOptionsMenu(menu, inflater);
}

View file

@ -216,7 +216,11 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment
binding.mapView.getZoomController()
.setVisibility(CustomZoomButtonsController.Visibility.NEVER);
binding.mapView.setMultiTouchControls(true);
binding.mapView.getController().setZoom(ZOOM_LEVEL);
if (!isCameFromNearbyMap()) {
binding.mapView.getController().setZoom(ZOOM_LEVEL);
}
performMapReadyActions();
binding.mapView.getOverlays().add(new MapEventsOverlay(new MapEventsReceiver() {
@ -344,7 +348,7 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment
moveCameraToPosition(
new GeoPoint(prevLatitude, prevLongitude),
prevZoom,
2L
1L
);
} else {
moveCameraToPosition(
@ -919,7 +923,9 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment
-0.07483536015053005, 1f);
}
}
moveCameraToPosition(new GeoPoint(latLnge.getLatitude(), latLnge.getLongitude()));
if (!isCameFromNearbyMap()) {
moveCameraToPosition(new GeoPoint(latLnge.getLatitude(), latLnge.getLongitude()));
}
return latLnge;
}

View file

@ -493,7 +493,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
moveCameraToPosition(
new GeoPoint(prevLatitude, prevLongitude),
prevZoom,
2L
1L
);
}
binding.map.getOverlays().add(mapEventsOverlay);