0

I'm iOS guy try to move to Android, I need someone to explain/help me what should I do in this situation. I have an app with tabhost fragment(3 tabs). At this moment, I just load 3 different simple fragments, it works fine.

Now, I want to implement MapActivity onto the first tab. Unfortunately, I can't extends it to MapActivity. It is currently extended to Fragment.

I searched over internet, but could find out the perfect solution work around it.

This link is one of solution, but it uses LocalActivityManager which is deprecated.

My question is Can I just load 2 fragments and one separate activity for map into fragment tabhost. If its possible, can you please walk me through how to implement that?

NOTE: I built my app tabhost fragment base on this tutorial.

Million thanks from me.

JHHoang
  • 653
  • 1
  • 8
  • 22

1 Answers1

1

I think that one solution would be using this modified version of the support lib there FragmentActivity extends MapActivity instead of Activity:

https://github.com/petedoyle/android-support-v4-googlemaps

Remember that you need to create the MapView object inside the Activity class and pass it to the fragment, in this way:

public class MyMapActivity extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Exchanger.sMapView = new MapView(this, "your_api_key");

    }

    public static class Exchanger {
        public static MapView sMapView;
    }
}

and the Fragment will be

public class MapFragment extends Fragment {

    @Override
    public void onCreate(Bundle args) {
        super.onCreate(args);
        setRetainInstance(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup vg, Bundle data) {

        final ViewGroup parent = (ViewGroup) MyMapActivity.Exchanger.sMapView.getParent();
        if (parent != null)
            parent.removeView(MyMapActivity.Exchanger.sMapView);

        return MyMapActivity.Exchanger.mMapView;
    }

}

MyMapActivity is the TabsFragmentActivity in the tutorial you linked. Remember to use the modified version of the support lib (download link)

The above method is now deprecated. You can now use a MapFragment: http://developer.android.com/google/play-services/maps.html

rciovati
  • 27,603
  • 6
  • 82
  • 101
  • Thanks I'm trying to implement it now, I have 1 more question is do I need to delete the old support lib? – JHHoang Aug 01 '12 at 14:13
  • Can you explain this line: Exchanger.sMapView = new MapView(this, getString(R.string.gmap_api_key)); – JHHoang Aug 01 '12 at 14:18
  • You create a MapView object using the API key (i put it in the string.xml file ma but your are free to hard-code it). To obtain api key see here: https://developers.google.com/maps/documentation/android/mapkey – rciovati Aug 01 '12 at 14:25
  • As the way you said, I dont need and XML layout hold mapView where I usually include map API key. Am I correct? Also I see you return return MyMapActivity.Exchanger.mMapView; what is mMapView? is it sMapView? – JHHoang Aug 01 '12 at 14:31
  • Last question, if I want to add drop pins on the map, then I do it in my Mapfragment right? – JHHoang Aug 01 '12 at 14:41
  • Yes, all kind of overlay you need to add it in your fragment. – rciovati Aug 01 '12 at 14:44