react-native-navigation的迁移库

NavigationApplication.java 3.1KB

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