问题
So in my main activity I have instantiated a GoogleMap object.
On button click, in my next Acivity, I also have a GoogleMap object.
I want to know if it's possible to use the same object and not have to create a new GoogleMap object and do the appropriate set-up again.
Basically I don't want to do the same thing twice in two different activities and take the performance hit.
I know it's possible to move data via intents, extras, bundles etc. but I can't find information regarding GoogleMaps or passing actual non-stringified objects.
Cheers.
回答1:
Even if you can pass the object to another activity, you need to have another instance of map xml in your second activity which will have a reference to google map object. Another solution is to have a static object of map which is not a good practice and I wont recommend it.
回答2:
I think you can avoid all the hassle of serializing the object using https://github.com/greenrobot/EventBus
Using EventBus you should be able to do something like:
Activity A {
GoogleMap mMap;
...
// just before moving to next Activity
EventBus.getDefault().postSticky(map);
}
Activity B {
GoogleMap mMap;
void onCreate() {
mMap = EventBus.getDefault().getStickyEvent(GoogleMap.class);
}
}
Not only is it simpler, it actually outperforms any other alternatives such as parcelable etc.
I must admit that I do not have had the need of reusing GoogleMap across Activities, but I have successfully reused between orientation changes to avoid having to reload all my markers, setting target location, zoom level and so on.
来源:https://stackoverflow.com/questions/25517406/is-it-possible-to-move-a-googlemap-object-from-one-activity-to-another