我在一个带有下拉导航的活动中使用Google Maps API V2,其中地图位于第二个位置。
我正在通过编程方式添加地图,如下:
mMapFragment = supportMapFragment.newInstance();
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.placeHolder, mMapFragment, TAG_MAP)
.commit();
我想获取GoogleMap对象,根据文档https://developers.google.com/maps/documentation/android/map,应该使用mMapFragment.getMap()
,但是它返回null。
根据http://developer.android.com/reference/com/google/android/gms/maps/SupportMapFragment.html,如果片段没有经历onCreateView生命周期事件,则返回null。
如何知道片段何时准备好?
编辑:我发现这个How do I know the map is ready to get used when using the SupportMapFragment?
覆盖onActivityCreated似乎是一种解决方案,但是那样我就必须通过构造函数实例化片段,而不是使用newInstance(),这有任何区别吗?