Skip to content Skip to sidebar Skip to footer

Is It Possible To Move A Googlemap Object From One Activity To Another?

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

Solution 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.

Solution 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;

    voidonCreate() {
        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.

Post a Comment for "Is It Possible To Move A Googlemap Object From One Activity To Another?"