android / architecture-components-samples

Samples for Android Architecture Components.
https://d.android.com/arch
Apache License 2.0
23.4k stars 8.29k forks source link

How to save maps instance? #978

Closed kryakushin666 closed 2 years ago

kryakushin666 commented 3 years ago

Hello, I use google maps api and I want to save map instance, because everytime I go to another screen and back, the map is updated instead of staying in the same place, because of this, there is a big cheat of opening times. How I can fix this?

hoc081098 commented 3 years ago

You can save Marker Location in ViewModel

SinaElahimanesh commented 3 years ago

"The Best Way" to handle the applications each has an activity which has Maps, is to make that activity Singleton. It means you can constraint that activity to JUST ONE TIME INSTANTIATE and so that just once this activity creates and because of that it doesn't make a problem about instantiating again it. So I can say the solution is abbreviated in this step: 1- Make the Activity Singleton

And the code is as bellow: Android Studio -> app -> manifests -> AndroidManifest.xml

Screen Shot 2021-05-17 at 8 04 30 PM

As show in the picture, you should add bellow code to your Map Activity at Manifest android:launchMode="singleTask" It will make that Activity instantiate just once and so your app just loads the map once and because of that the state of the application in the process of moving between different activities (and as you said Screens) save. Because of that the problem will be fixed and all states of the application saved, So if you change the screen, you will stay in the same place after you return to Map Activity.

The last thing about this problem is about the Location. In this case the Last Location of your Map will be saved and if you come back to this activity, there will be no problem about that!

Rishavgupta12345 commented 3 years ago

can i work on this issu ?

this is my first contribution