android - How do I insert MapsActivity into a Fragment Container? -
i have code on default mapsactivity:
import android.support.v4.app.fragmentactivity; import android.os.bundle; import com.google.android.gms.maps.cameraupdatefactory; import com.google.android.gms.maps.googlemap; import com.google.android.gms.maps.onmapreadycallback; import com.google.android.gms.maps.supportmapfragment; import com.google.android.gms.maps.model.latlng; import com.google.android.gms.maps.model.markeroptions; public class mapsactivity extends fragmentactivity implements onmapreadycallback { private googlemap mmap; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_maps); // obtain supportmapfragment , notified when map ready used. supportmapfragment mapfragment = (supportmapfragment) getsupportfragmentmanager() .findfragmentbyid(r.id.map); mapfragment.getmapasync(this); }
and on navigation drawer mainactivity
mapsactivity fragment = new mapsactivity(); fragmenttransaction fragmenttransaction = getsupportfragmentmanager().begintransaction(); fragmenttransaction.replace(r.id.fragment_container, fragment); fragmenttransaction.commit();
the second code can manage make work other fragments except 1 maps. wrong?
if want control supportmapfragment activity, the answer cricket_007 work fine.
if want custom functionality, , want keep in fragment, can create fragment extends supportmapfragment, example:
public class mycustommapfragment extends supportmapfragment implements onmapreadycallback { private googlemap mmap; public mycustommapfragment () { } @override public void onresume() { super.onresume(); setupmapifneeded(); } private void setupmapifneeded() { if (mmap == null) { getmapasync(this); } } @override public void onmapready(googlemap googlemap) { mmap = googlemap; mmap.setmylocationenabled(true); mmap.setmaptype(googlemap.map_type_hybrid); mmap.getuisettings().setmaptoolbarenabled(false); } }
then can use fragmenttransaction replace container custom supportmapfragment:
mycustommapfragment fragment = new mycustommapfragment(); fragmenttransaction fragmenttransaction = getsupportfragmentmanager().begintransaction(); fragmenttransaction.replace(r.id.fragment_container, fragment); fragmenttransaction.commit();