6
6
import com .facebook .react .bridge .ReactApplicationContext ;
7
7
import com .facebook .react .bridge .ReactMethod ;
8
8
9
+ import com .facebook .react .bridge .ReactContextBaseJavaModule ;
10
+ import com .facebook .react .module .annotations .ReactModule ;
11
+ import io .flutter .embedding .android .FlutterActivity ;
12
+ import io .flutter .embedding .engine .FlutterEngine ;
13
+ import io .flutter .embedding .engine .FlutterEngineCache ;
14
+ import io .flutter .embedding .engine .dart .DartExecutor ;
15
+ import android .app .Activity ;
16
+
9
17
public class EnsembleReactNativeModule extends EnsembleReactNativeSpec {
10
18
public static final String NAME = "EnsembleReactNative" ;
19
+ private final ReactApplicationContext reactContext ;
20
+ public FlutterEngine flutterEngine ;
11
21
12
22
EnsembleReactNativeModule (ReactApplicationContext context ) {
13
23
super (context );
24
+ this .reactContext = context ;
25
+ new Thread (new Runnable () {
26
+ @ Override
27
+ public void run () {
28
+ try {
29
+ //dummy delay
30
+ Thread .sleep (100 );
31
+ } catch (InterruptedException e ) {
32
+ e .printStackTrace ();
33
+ }
34
+
35
+ //Update ui on UI thread
36
+ reactContext .getCurrentActivity ().runOnUiThread (new Runnable () {
37
+ @ Override
38
+ public void run () {
39
+ // Instantiate a FlutterEngine.
40
+ flutterEngine = new FlutterEngine (reactContext .getApplicationContext ());
41
+
42
+ // Start executing Dart code to pre-warm the FlutterEngine.
43
+ flutterEngine .getDartExecutor ().executeDartEntrypoint (
44
+ DartExecutor .DartEntrypoint .createDefault ()
45
+ );
46
+
47
+ // Cache the FlutterEngine to be used by FlutterActivity.
48
+ FlutterEngineCache
49
+ .getInstance ()
50
+ .put (NAME , flutterEngine );
51
+ }
52
+ });
53
+
54
+ }
55
+ }).start ();
14
56
}
15
57
16
58
@ Override
@@ -20,10 +62,19 @@ public String getName() {
20
62
}
21
63
22
64
23
- // Example method
24
- // See https://reactnative.dev/docs/native-modules-android
25
65
@ ReactMethod
26
66
public void multiply (double a , double b , Promise promise ) {
27
67
promise .resolve (a * b );
28
68
}
69
+
70
+ @ ReactMethod
71
+ public void openEnsembleApp (Promise promise ) {
72
+ Activity currentActivity = reactContext .getCurrentActivity ();
73
+
74
+ currentActivity .startActivity (
75
+ FlutterActivity
76
+ .withCachedEngine (NAME )
77
+ .build (currentActivity )
78
+ );
79
+ }
29
80
}
0 commit comments