@ReactMethod void callJavaScript() { Activity activity = getCurrentActivity(); if (activity != null) { MainApplication application = (MainApplication) activity.getApplication(); ReactNativeHost reactNativeHost = application.getReactNativeHost(); ReactInstanceManager reactInstanceManager = reactNativeHost.getReactInstanceManager(); ReactContext reactContext = reactInstanceManager.getCurrentReactContext(); if (reactContext != null) { CatalystInstance catalystInstance = reactContext.getCatalystInstance(); WritableNativeArray params = new WritableNativeArray(); params.pushString("Hello, JavaScript!"); catalystInstance.callFunction("JavaScriptVisibleToJava", "alert", params); } } }
public RNThreadModule(final ReactApplicationContext reactContext, ReactNativeHost reactNativehost, ReactPackage additionalThreadPackages[]) { super(reactContext); this.reactApplicationContext = reactContext; threads = new HashMap<>(); this.reactNativeHost = reactNativehost; this.additionalThreadPackages = additionalThreadPackages; reactContext.addLifecycleEventListener(this); }
/** * Demonstrates how to add a custom option to the dev menu. * https://stackoverflow.com/a/44882371/3968276 */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); MainApplication application = (MainApplication) getApplication(); ReactNativeHost reactNativeHost = application.getReactNativeHost(); ReactInstanceManager reactInstanceManager = reactNativeHost.getReactInstanceManager(); DevSupportManager devSupportManager = reactInstanceManager.getDevSupportManager(); devSupportManager.addCustomDevOption("Custom dev option", new DevOptionHandler() { @Override public void onOptionSelected() { Toast.makeText(MainActivity.this, "Hello from custom dev option", Toast.LENGTH_SHORT).show(); } }); }
@Nullable @Override protected HeadlessJsTaskConfig getTaskConfig(Intent intent) { Log.d(LOG_TAG, "getTaskConfig() called with: intent = [" + intent + "]"); Bundle extras = intent.getExtras(); boolean allowExecutionInForeground = extras.getBoolean("allowExecutionInForeground", false); long timeout = extras.getLong("timeout", 2000); // For task with quick execution period additional check is required ReactNativeHost reactNativeHost = ((ReactApplication) getApplicationContext()).getReactNativeHost(); boolean appInForeground = Utils.isReactNativeAppInForeground(reactNativeHost); if (appInForeground && !allowExecutionInForeground) { return null; } return new HeadlessJsTaskConfig(intent.getStringExtra("jobKey"), Arguments.fromBundle(extras), timeout, allowExecutionInForeground); }
/** Check whether on not the React Native application is in foreground. */ public static boolean isReactNativeAppInForeground(@NonNull ReactNativeHost reactNativeHost) { if (!reactNativeHost.hasInstance()) { // If the app was force-stopped the instace will be destroyed. The instance can't be created from a background thread. return false; } ReactContext reactContext = reactNativeHost.getReactInstanceManager().getCurrentReactContext(); return reactContext != null && reactContext.getLifecycleState() == LifecycleState.RESUMED; }
@Override public ReactNativeHost getReactNativeHost() { return mReactNativeHost; }
public RNThreadPackage(ReactNativeHost reactNativeHost, ReactPackage... additionalThreadPackages) { this.reactNativeHost = reactNativeHost; this.additionalThreadPackages = additionalThreadPackages; }