Question

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.

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top