java - How to implement google maps in a Fragment correctly? -
i have searched on stackoverflow , looked @ many solutions example, here
i have looked @ more none of them robust in terms of performance. here got far
this 1 works once when fragment first created after returning map fragment gives me layout inflation exception
@override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view=inflater.inflate(r.layout.fragment_public_maps,null,false); googlemap= (supportmapfragment) getchildfragmentmanager().findfragmentbyid(r.id.pub_map); googlemap.getmapasync(this); return view; }
i have tried 1 , works makes app slow , crashes
@override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { try { view=inflater.inflate(r.layout.fragment_public_maps,null,false); } catch (inflateexception e) { googlemap= (supportmapfragment) getchildfragmentmanager().findfragmentbyid(r.id.pub_map); googlemap.getmapasync(this); return view; } googlemap= (supportmapfragment) getchildfragmentmanager().findfragmentbyid(r.id.pub_map); googlemap.getmapasync(this); return view; }
i don't know else if can point me right direction appreciate it.
the way , works great place framelayout within fragments xml file so:
<framelayout android:id="@+id/map_id" android:layout_width="match_parent" android:layout_height="match_parent"/>
then in fragments onactivitycreated method this:
@override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); mmapfragment = mapfragment.newinstance(); if (build.version.sdk_int > build.version_codes.jelly_bean) { getchildfragmentmanager().begintransaction().replace(r.id.map_id, mmapfragment).commit(); } else { getfragmentmanager().begintransaction().replace(r.id.map_id, mmapfragment).commit(); } mmapfragment.getmapasync(this); }
if you're using support fragments,
@override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); mmapfragment = supportmapfragment.newinstance(); getsupportfragmentmanager().begintransaction().replace(r.id.sod_map_lite, mmapfragment).commit(); }
Comments
Post a Comment