0

I'am developping an android application including swipe view with tab. All page had his own layout, all of that is working great :D.

But I want add to one of this view a MapView ( from Google API ), so i need to extends my MainActivity ( the only one I have ) to MapActivity, but I can't ( nothing wrong here ):

 public class MainActivity extends ActionBarActivity implements ActionBar.TabListener{

so is there a way to manage my MapView from MainActivity ?

thanks a lot :)

( sorry for my poor english syntax )

2 Answers2

3

Often when you stumble across a problem like this, you need to look at encapsulation rather than extension. Can't you use the MapView as a member variable?

If you check out the MapView API, it states that we must implement the Android life cycle for the MapView in order for it to function properly. So, if you use a MapView as a member variable, you need to override the following methods in your main activity to call the matching methods in the MapView:

onCreate(Bundle)
onResume()
onPause()
onDestroy()
onSaveInstanceState(Bundle)
onLowMemory()

As an example, your Activity would look like the following:

public final class MainActivity extends Activity {

    private MapView mMapView;

    @Override
    public final void onCreate(final Bundle pSavedInstanceState) {
            super(pSavedInstanceState);
            this.mMapView = new MapView(this);
            /* Now delegate the event to the MapView. */
            this.mMapView.onCreate(pSavedInstanceState);
    }

    @Override
    public final void onResume() {
            super.onResume();
            this.getMapView().onResume();
    }

    @Override
    public final void onPause() {
            super.onPause();
            this.getMapView().onPause();
    }

    @Override
    public final void onDestroy() {
            super.onDestroy();
            this.getMapView().onDestroy();
    }

    @Override
    public final void onSaveInstanceState(final Bundle pSavedInstanceState) {
            super.onSaveInstanceState(pSavedInstanceState);
            this.getMapView().onSaveInstanceState(pSavedInstanceState);
    }

    @Override
    public final void onLowMemory() {
            super.onLowMemory();
            this.getMapView().onLowMemory();
    }

    private final MapView getMapView() {
            return this.mMapView;
    }

}
Mapsy
  • 4,192
  • 1
  • 37
  • 43
0

The answer is no. It is not possible to utilize a MapView without extending MapActivity. As gulbrandr pointed out there is a similar question here, this may be of use for anyone attempting the same thing that I was.

Community
  • 1
  • 1
Mahmoud
  • 353
  • 1
  • 3
  • 14
  • So i can use MapFragment ? thanks a lot, I test that right now :) – Alexandre Nicolas Feb 16 '14 at 14:43
  • 1
    `MapActivity` is for Maps V1, which has been deprecated for ~16 months and can no longer be used for new development. Maps V2, the current map API, does not have a `MapActivity` at all, let alone a requirement to use one. – CommonsWare Feb 16 '14 at 15:02