1

I am trying to use MapView with singleton approach.

Here I create MapView once and does its mapViewSingletonInstance.onCreate(savedInstance) only one time. After this I try to use it doing anotherMapViewObject.addView(mapViewSingletonInstance)

But this only works one time. After adding to one mapView with anotherMapView it does not work.

public class GoogleMapSingleton{
        private static GoogleMapSingleton googleMapSingleton;
        private static MapView singletonMapView;


private GoogleMapSingleton()
{

}
public static GoogleMapSingleton GoogleMapSingletonInstance(Bundle savedInstanceState, Context context)
{

    if(googleMapSingleton == null){

        singletonMapView = new MapView(context);
        LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        singletonMapView.setLayoutParams(lp);
        singletonMapView.onCreate(savedInstanceState);
        singletonMapView.onResume();
        singletonMapView.getMap();
        googleMapSingleton = new GoogleMapSingleton();

    }

    return googleMapSingleton;
}

/**
 * @return the singletonMapView
 */
public MapView getSingletonMapView() {
    return singletonMapView;
}


}
JasonWilczak
  • 2,303
  • 2
  • 21
  • 37
Devavrata
  • 301
  • 1
  • 3
  • 12
  • Perhaps you should move the first 6 lines after null check to the constructor and only leave the new GoogleMapSingle() there. that way you will have a cleaner getInstance method. – Thupten Jun 12 '15 at 13:12
  • @Thupten I kept those 6 lines inside the null check so it happens only once along with singleton object creation and thereafter can be used anywhere (The reason for doing so is google charge amount everytime mapView.onCreate(savedInstance) method gets called). If I will put outside then google will charge the mapView load again and again. – Devavrata Jun 15 '15 at 05:47

0 Answers0