react-native-navigation的迁移库

NavigationApplication.java 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package com.reactnativenavigation;
  2. import android.app.Application;
  3. import com.facebook.react.ReactApplication;
  4. import com.facebook.react.ReactNativeHost;
  5. import com.facebook.soloader.SoLoader;
  6. import com.reactnativenavigation.react.ReactGateway;
  7. import com.reactnativenavigation.viewcontrollers.externalcomponent.ExternalComponentCreator;
  8. import java.util.HashMap;
  9. import java.util.Map;
  10. import androidx.annotation.NonNull;
  11. public abstract class NavigationApplication extends Application implements ReactApplication {
  12. private ReactGateway reactGateway;
  13. public static NavigationApplication instance;
  14. final Map<String, ExternalComponentCreator> externalComponents = new HashMap<>();
  15. @Override
  16. public void onCreate() {
  17. super.onCreate();
  18. instance = this;
  19. SoLoader.init(this, false);
  20. reactGateway = createReactGateway();
  21. }
  22. /**
  23. * Subclasses of NavigationApplication may override this method to create the singleton instance
  24. * of {@link ReactGateway}. For example, subclasses may wish to provide a custom {@link ReactNativeHost}
  25. * with the ReactGateway. This method will be called exactly once, in the application's {@link #onCreate()} method.
  26. *
  27. * Custom {@link ReactNativeHost}s must be sure to include {@link com.reactnativenavigation.react.NavigationPackage}
  28. *
  29. * @return a singleton {@link ReactGateway}
  30. */
  31. protected ReactGateway createReactGateway() {
  32. return new ReactGateway(getReactNativeHost());
  33. }
  34. public ReactGateway getReactGateway() {
  35. return reactGateway;
  36. }
  37. /**
  38. * Generally no need to override this; override for custom permission handling.
  39. */
  40. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  41. }
  42. /**
  43. * Register a native View which can be displayed using the given {@code name}
  44. * @param name Unique name used to register the native view
  45. * @param creator Used to create the view at runtime
  46. */
  47. @SuppressWarnings("unused")
  48. public void registerExternalComponent(String name, ExternalComponentCreator creator) {
  49. if (externalComponents.containsKey(name)) {
  50. throw new RuntimeException("A component has already been registered with this name: " + name);
  51. }
  52. externalComponents.put(name, creator);
  53. }
  54. public final Map<String, ExternalComponentCreator> getExternalComponents() {
  55. return externalComponents;
  56. }
  57. }