如何管理 React Native 的生命周期

Ric*_*eet 5 react-native

我知道当我们需要一个 nativeUI 组件时,我们需要覆盖函数 \n getName()\n createViewInstance(ThemedReactContext context)但是当我使用关于 map 的依赖时。API 需要我使用这样的组件

\n\n
    @Override  \nprotected void onDestroy() {  \n    super.onDestroy();  \n    //\xe5\x9c\xa8activity\xe6\x89\xa7\xe8\xa1\x8conDestroy\xe6\x97\xb6\xe6\x89\xa7\xe8\xa1\x8cmMapView.onDestroy()\xef\xbc\x8c \n    mMapView.onDestroy();  \n}  \n@Override  \nprotected void onResume() {  \n    super.onResume();  \n    //\xe5\x9c\xa8activity\xe6\x89\xa7\xe8\xa1\x8conResume\xe6\x97\xb6\xe6\x89\xa7\xe8\xa1\x8cmMapView. onResume ()\xef\xbc\x8c \n    mMapView.onResume();  \n    }  \n@Override  \nprotected void onPause() {  \n    super.onPause();  \n    //\xe5\x9c\xa8activity\xe6\x89\xa7\xe8\xa1\x8conPause\xe6\x97\xb6\xe6\x89\xa7\xe8\xa1\x8cmMapView. onPause ()\xef\xbc\x8c  \n    mMapView.onPause();  \n    }  \n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我重写了这个函数getetName()\n createViewInstance(ThemedReactContext context)就像这样

\n\n
    @Override\npublic String getName() {\n    return REACT_CLASS;\n}\n\n@Override\npublic MapView createViewInstance(ThemedReactContext context) {\n        final ThemedReactContext mapContext = context;\n        bdMapViewInstance = new MapView(context);\n        bdMapViewInstance.getMap().setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {\n            @Override\n            public boolean onMarkerClick(Marker marker) {\n                ShopResponseInfo shopResponseInfo = (ShopResponseInfo) marker.getExtraInfo().getSerializable(INDENTIFY);\n                if(shopResponseInfo != null){\n                    String id = shopResponseInfo.getShop_id() + "";\n                    String shop_name = shopResponseInfo.getShop_name() + "";\n                    onReceiveNativeEvent(mapContext,bdMapViewInstance,id,shop_name);\n                }\n                return true;\n            }\n        });\n    return bdMapViewInstance;\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

最后,我的应用程序存在一些性能问题。我不知道这是否会影响我的应用程序的性能。而且我不知道如何满足官方的建议。我不知道如何控制android的nativeUI组件的生命周期。很感谢。

\n

age*_*unt 8

您可以在 createViewInstance 中像这样监听 Activity 生命周期。您可能希望跟踪侦听器并根据您对实例的跟踪适当地删除它们。

LifecycleEventListener lifecycleEventListener = new LifecycleEventListener() {
  @Override
  public void onHostResume() {

  }

  @Override
  public void onHostPause() {

  }

  @Override
  public void onHostDestroy() {

  }
};
getReactApplicationContext().addLifecycleEventListener(lifecycleEventListener);
Run Code Online (Sandbox Code Playgroud)