Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Google Maps Wont restoreState properly when navigating through bottom nav #112

Closed
basurahan opened this issue May 7, 2022 · 10 comments · Fixed by #137
Closed

Google Maps Wont restoreState properly when navigating through bottom nav #112

basurahan opened this issue May 7, 2022 · 10 comments · Fixed by #137
Assignees
Labels
released triage me I really want to be triaged. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@basurahan
Copy link

Environment details

  1. library: com.google.maps.android:maps-compose:2.1.0

Steps to reproduce

  1. clone this repo
  2. add your google api key to the manifest file
  3. Quickly navigate between the bottom tabs and check if the maps is loaded properly
  4. When navigating quickly with the bottom tabs the maps wont restore its state eg. camera position, markers

Observation

The maps will load properly even if we navigate quickly on the bottom tabs when the starting route of nav host is the map to reproduce.

  1. Change the AnimatedNavHost start destination from
AnimatedNavHost(
         navController = navController,
         startDestination = Screen.Other.route
     )

to

AnimatedNavHost(
            navController = navController,
            startDestination = Screen.Map.route
        )

and it will work just fine.

Here is a sample video of the bug

@basurahan basurahan added triage me I really want to be triaged. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. labels May 7, 2022
@jpoehnelt
Copy link
Contributor

@thekingrenz23 Thank you for opening this issue. 🙏
Please check out these other resources that might be applicable:

This is an automated message, feel free to ignore.

@basurahan
Copy link
Author

Still no upadate on this one?

@arriolac
Copy link
Contributor

I can confirm the bug—thanks for reporting @thekingrenz23. The bug isn't immediately obvious so will need to do add some additional logging to pinpoint the issue.

@arriolac
Copy link
Contributor

I tested a different scenario where a button toggles a mutableStateOf boolean that brings the GoogleMap composable in and out of composition. Quickly tapping the button doesn't produce the same bug and so it looks like the issue is specific to the GoogleMap being used within a BottomNavigation composable (along with the specific config you have set up in your sample).

@PiotrPrus
Copy link

PiotrPrus commented May 22, 2022

You can reduce the occurrence of this error by omitting the default cross-fade transition for navHost. You can do this using : https://google.github.io/accompanist/navigation-animation/
here is the snipped:
AnimatedNavHost( navController = navController, startDestination = "home", enterTransition = { EnterTransition.None }, exitTransition = { ExitTransition.None } )

@basurahan
Copy link
Author

hmmm I tried it with the default NavHost with no animation capability and still happens so it must not be the animation I guess?

@PiotrPrus
Copy link

The default NavHost has a crossfade animation built in.

@basurahan
Copy link
Author

Removing the animation is not an option for my project

@arriolac
Copy link
Contributor

arriolac commented Jun 7, 2022

Looks like this issue happens you're in the tab displaying the map, and quickly tap out and back in (before the crossfade animation completes). In this specific scenario, the MapView is in the state such that the GoogleMap composable never left the composition (and so nodes aren't readded) but the MapView has been recreated (i.e. went through an ON_STOP, followed by an ON_CREATE > ON_START > ON_RESUME). I believe the fix here would be to propagate the saved instance state Bundle across the various MapView lifecycle events. However, there are no hooks in the androidx.lifecycle library to obtain that bundle. Will try to figure out a workaround for this.

Edit: saving instance state will only restore some properties of the map (like the camera's position). Drawn objects like markers will have to be readded. The closest thing to a workaround for now is to shorten the crossfade duration to reduce the occurrence of this bug.

@googlemaps-bot
Copy link
Contributor

🎉 This issue has been resolved in version 2.2.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
released triage me I really want to be triaged. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
6 participants