java - How to implement google maps in a Fragment correctly? -


i have searched on stackoverflow , looked @ many solutions example, here

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

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -