I have a fragment activity that switches fragments on button click.
The two fragments that I switch are:
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?
Use replace() to replace an existing fragment in a container with an instance of a new fragment class that you provide. Calling replace() is equivalent to calling remove() with a fragment in a container and adding a new fragment to that same container. transaction. commit();
beginTransaction(). remove(fragment). commit(); That is it.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With