问题
I'm using a single-activity architecture, with Fragments as screen, and using the Navigation component. In the first fragment, I initialize the map in onViewCreated and destroy it in onDestroyView.
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
mapView.onCreate(savedInstanceState)
loadMapScene()
}
override fun onDestroyView() {
mapView?.onDestroy()
super.onDestroyView()
}
From this fragment, user can navigate to another fragment with a map, which also calls mapView.onCreate in its onViewCreated. However, using the newest (4.4.0.2.2340) Here Maps Lite library it crashes, because:
MapViewLite.class
private static boolean mInstanceActivated;
public void onCreate(@Nullable Bundle var1) {
if (!mInstanceActivated) {
//various initializations
mInstanceActivated = true;
} else {
throw new RuntimeException("Only one MapViewLite instance can be created at a time");
}
}
public void onDestroy() {
this.mNativeRenderer.dispose();
this.mNativeRenderer = null;
// setting to nulls
mInstanceActivated = false;
}
In the normal Fragment lifecycle, onViewCreated of the fragment navigated to happens before even onPause (not to mention onDestroyView) of the fragment navigated from. That means the second mapView.onCreate is called before the first mapView.onDestroy() and thus its RuntimeException is thrown. Interestingly, it didn't crash in the 4.3.3.0.2052 version (to which I reverted for now). Could you please make the Here Lite MapView usable in a single Activity architecture?
来源:https://stackoverflow.com/questions/63048997/mapview-by-here-crashes-when-calling-mapview-oncreate-in-a-single-activity-archi