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 ;
@@ -16,9 +17,40 @@ public CodePushDialog(ReactApplicationContext reactContext) {
16
17
}
17
18
18
19
@ 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 ());
20
+ public void showDialog (final String title , final String message , final String button1Text ,
21
+ final String button2Text , final Callback successCallback , Callback errorCallback ) {
22
+ Activity currentActivity = getCurrentActivity ();
23
+ if (currentActivity == null ) {
24
+ // If getCurrentActivity is null, it could be because the app is backgrounded,
25
+ // so we show the dialog when the app resumes)
26
+ getReactApplicationContext ().addLifecycleEventListener (new LifecycleEventListener () {
27
+ @ Override
28
+ public void onHostResume () {
29
+ Activity currentActivity = getCurrentActivity ();
30
+ if (currentActivity != null ) {
31
+ getReactApplicationContext ().removeLifecycleEventListener (this );
32
+ showDialogInternal (title , message , button1Text , button2Text , successCallback , currentActivity );
33
+ }
34
+ }
35
+
36
+ @ Override
37
+ public void onHostPause () {
38
+
39
+ }
40
+
41
+ @ Override
42
+ public void onHostDestroy () {
43
+
44
+ }
45
+ });
46
+ } else {
47
+ showDialogInternal (title , message , button1Text , button2Text , successCallback , currentActivity );
48
+ }
49
+ }
50
+
51
+ private void showDialogInternal (String title , String message , String button1Text ,
52
+ String button2Text , final Callback successCallback , Activity currentActivity ) {
53
+ AlertDialog .Builder builder = new AlertDialog .Builder (currentActivity );
22
54
23
55
builder .setCancelable (false );
24
56
0 commit comments