我有一个Activity并将两个片段(MapScreenFragment和OtherFragment)加载到backstack。我遇到的问题是,当我开始从backstack弹出一些片段时,MapScreenFragment显示黑屏。
我应该将新地图设置为MapView吗?
map = new Map();
mapView.setMap(map);
XML布局
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.here.android.mpa.mapping.MapView
android:id="@+id/map_view"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</FrameLayout>
片段类
public class MapScreenFragment extends Fragment {
@Override
public void onResume() {
super.onResume();
Log.d(TAG, "onResume");
if(isMapEngineInitialized && mapEngine != null) {
mapEngine.onResume();
mapView.onResume();
Log.d(TAG, " onResume Map engine reference " + mapEngine.getResourceReferenceCount());
}
}
@Override
public void onPause() {
super.onPause();
Log.d(TAG, "onPause");
if(isMapEngineInitialized && mapEngine != null) {
mapEngine.onPause();
mapView.onPause();
Log.d(TAG, " onPause Map engine reference " + mapEngine.getResourceReferenceCount());
}
}
}
/***** added ******/
OnEngineInitListener onEngineInitListener = new OnEngineInitListener() {
@Override
public void onEngineInitializationCompleted(Error error) {
if(error == Error.NONE) {
isMapEngineInitialized = true;
if(map == null) {
Log.d(TAG, " Map is null");
map = new Map();
}
mapView.setMap(map);
mapView.setMapMarkerDragListener(MapScreenFragment.this);
map = mapView.getMap();
map.addTransformListener(MapScreenFragment.this);
mapGesture = mapView.getMapGesture();
mapGesture.setAllGesturesEnabled(true);
mapGesture.addOnGestureListener(MapScreenFragment.this);
setUpMap();
Log.d(TAG, "MapEngine initialized! " + mapEngine.getResourceReferenceCount());
}
}
};
MainActivity Backpressed并添加片段
mapFragment = new MapScreenFragment ();
mFragmentTransaction = getSupportFragmentManager().beginTransaction();
mFragmentTransaction.add(R.id.main_frame_layout, mapFragment , mdmFragmentID);
mFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
mFragmentTransaction.addToBackStack(null);
mFragmentTransaction.commit();
otherFragment = new OtherFragment ();
mFragmentTransaction = getSupportFragmentManager().beginTransaction();
mFragmentTransaction.replace(R.id.main_frame_layout, otherFragment , mdmFragmentID);
mFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
mFragmentTransaction.addToBackStack(null);mFragmentTransaction.commit();
@Override
public void onBackPressed() {
Log.d(TAG, "Back stack count " + getSupportFragmentManager().getBackStackEntryCount());
if( getSupportFragmentManager().getBackStackEntryCount() == 1) {
finish();
} else {
getSupportFragmentManager().popBackStack();
}
}
答案 0 :(得分:1)
确保为isMapEngineInitialized == false调用MapView#onResume(),尤其是在调用MapEngine#onEngineInit侦听器时。
通常,应用程序生命周期比引擎初始化时更早地调用onResume()。如果未调用MapView#onResume(),则不会启动基础渲染构造。请参阅我们的示例应用程序以获取示例。
另外,为什么不使用嵌套片段?您始终可以将MapFragment嵌入到另一个片段中,以便自动处理生命周期。