5
5
import android .content .DialogInterface ;
6
6
7
7
import com .facebook .react .bridge .Callback ;
8
+ import com .facebook .react .bridge .LifecycleEventListener ;
8
9
import com .facebook .react .bridge .ReactApplicationContext ;
9
10
import com .facebook .react .bridge .ReactContextBaseJavaModule ;
10
11
import com .facebook .react .bridge .ReactMethod ;
@@ -15,10 +16,49 @@ public CodePushDialog(ReactApplicationContext reactContext) {
15
16
super (reactContext );
16
17
}
17
18
19
+ private LifecycleEventListener mLifecycleEventListener = null ;
20
+
18
21
@ ReactMethod
19
- public void showDialog (String title , String message , String button1Text , String button2Text ,
20
- final Callback successCallback , Callback errorCallback ) {
21
- AlertDialog .Builder builder = new AlertDialog .Builder (getCurrentActivity ());
22
+ public void showDialog (final String title , final String message , final String button1Text ,
23
+ final String button2Text , final Callback successCallback , Callback errorCallback ) {
24
+ Activity currentActivity = getCurrentActivity ();
25
+ if (currentActivity == null ) {
26
+ // If getCurrentActivity is null, it could be because the app is backgrounded,
27
+ // so we show the dialog when the app resumes)
28
+ mLifecycleEventListener = new LifecycleEventListener () {
29
+ private boolean shown = false ;
30
+
31
+ @ Override
32
+ public void onHostResume () {
33
+ Activity currentActivity = getCurrentActivity ();
34
+ if (!shown && currentActivity != null ) {
35
+ shown = true ;
36
+
37
+ // Set to null to allow GC.
38
+ mLifecycleEventListener = null ;
39
+
40
+ showDialogInternal (title , message , button1Text , button2Text , successCallback , currentActivity );
41
+ }
42
+ }
43
+
44
+ @ Override
45
+ public void onHostPause () {
46
+
47
+ }
48
+
49
+ @ Override
50
+ public void onHostDestroy () {
51
+
52
+ }
53
+ };
54
+ } else {
55
+ showDialogInternal (title , message , button1Text , button2Text , successCallback , currentActivity );
56
+ }
57
+ }
58
+
59
+ private void showDialogInternal (String title , String message , String button1Text ,
60
+ String button2Text , final Callback successCallback , Activity currentActivity ) {
61
+ AlertDialog .Builder builder = new AlertDialog .Builder (currentActivity );
22
62
23
63
builder .setCancelable (false );
24
64
0 commit comments