'MapFragment becomes slow after navigating back to it from a preferencesFragment

I added a Preferences Fragment to an application. I am using an ActionBar drop-down menu to navigate to the Preferences Fragment and also using the ActionBar's OnUpNavigation button to return to the previously displayed fragment which uses the OnBackPressed method to go back.

The application also has a BottomNavigationView which is controlled by a NavController and one of the Fragments attached to the BottomNavigationView is a MapFragment. All the navigations from Fragment to Fragment are handled by a NavigationController.

My issue is that if I navigate to the Preferences Fragment from the MapFragment and after I return back to the MapFragment, then the map becomes half-way unresponsive and has a lot of lag. After the map becomes slow and unresponsive, if I navigate to a different Fragment and back to the MapFragment, then the lag is gone and the MapFragment becomes responsive again.

The unresponsive/laggy behavior seems to only effect the MapFragment. Now I figured that maybe the Preferences Fragment might still stay active in the background so I also tried using the popBackStack() method directly, but the result is the same. I found another question here that was about a similar problem Google Maps v2 MapFragment is extremely laggy on returning from the backstack but that didn't solve my issue.

To navigate to the Preferences

 @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.settings:
                navController.navigate(R.id.settingsFragment);
                navView.setVisibility(View.GONE);
                return true;
                ...

Handling the back pressed button.

 @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return true;
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
            navView.setVisibility(View.VISIBLE);

    }

Additionally, here is the mobile navigation xml.

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mobile_navigation"
    app:startDestination="@+id/navigation_home">

    <fragment
        android:id="@+id/navigation_map"
        android:name="com.example.explore.ui.map.MapFragment"
        android:label="@string/title_map"
        tools:layout="@layout/fragment_map" />

    <fragment
        android:id="@+id/settingsFragment"
        android:name="com.example.explore.SettingsFragment"
        android:label="@string/title_settings"/>

    <!--Global action-->
    <action android:id="@+id/open_settings_fragment"
        app:destination="@id/settingsFragment"/>
</navigation>

If anything was unclear then please specify. Thank you!



Solution 1:[1]

I did not manage to figure out how to fix the issue with the unresponsive behavior after navigating back to the previous fragment, but I did find a workaround.

Basically I just got the previous BackStack entry, then from that entry i got the destination of that entry and then got the Id of that entry to navigated to the previous fragment using that Id.

    @Override
    public boolean onSupportNavigateUp() {
        NavBackStackEntry navBackStackEntry = navController.getPreviousBackStackEntry();
        NavDestination navDestination = navBackStackEntry.getDestination();
        int id = navDestination.getId();
        navController.navigate(id);
        return true;
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Edward Aarma