Switching from Map fragment to another Fragment and back

android, android-fragments, google-maps

Solution

I suspect this is happening to you because you have your map fragment defined in your XML layout file. In order to swap fragments in this way, you will need to instantiate your map in code, something like the following:

private void initializeMap() {

    mMapFragment = SupportMapFragment.newInstance();
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.add(R.id.fragment_container, mMapFragment, "map");
    fragmentTransaction.commit();

    handler.post(new Runnable() {
        @Override
        public void run() {
            mMap = mMapFragment.getMap();

            mMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
                @Override
                public void onCameraChange(CameraPosition cameraPosition) {
                    // Do something here
                }
            });
        }
    });
}

I found that `mMapFragment.getMap()` was returning null, so that's why you'll need to schedule the rest of the setup through a `Handler` instance. My layout XML looks like the following:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LauncherActivity">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fragment_container">

    <!-- The fragments will go here -->
</RelativeLayout>

<RelativeLayout
    android:layout_gravity="start"
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    android:background="@color/drawer_background">
...
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>

I hope this helps.

Problem

I have a fragment activity that switches fragments on button click. The two fragments that I switch are: - Google map android v2 fragment - Another fragment with a text view This is how I switch the fragments: ``` public void onClick(View v) { FragmentManager fm = getSupportFragmentManager(); if(tv.getText().toString().equalsIgnoreCase("Click ME!!")){ tv.setText("MAP"); if (fm != null) { if(map == null){ map = new Map(); } FragmentTransaction ft = fm.beginTransaction(); ft.replace(R.id.lay_contaier, map); ft.commit(); } } else{ tv.setText("Click ME!!"); if (fm != null ){ FragmentTransaction ft = fm.beginTransaction(); ft.replace(R.id.lay_contaier,new empty()); ft.commit(); } } } ``` This s my Maps Fagment code ``` public class Map extends Fragment implements OnMarkerClickListener, OnInfoWindowClickListener, OnMarkerDragListener { private static final LatLng PERTH = new LatLng(-31.952854, 115.857342); private static final LatLng SYDNEY = new LatLng(-33.87365, 151.20689); private GoogleMap mMap; private Marker mPerth; private Marker mSydney; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.activity_main,null); return view; } ``` activity_mail.xml ``` <?xml version="1.0" encoding="utf-8"?> <fragment xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/map" android:layout_width="fill_parent" android:layout_height="fill_parent" class="com.google.android.gms.maps.SupportMapFragment"/> ``` But every time I swich back to the Map fragment the Application crashes. How should I make this code right?

Original source