55import android .content .DialogInterface ;
66
77import com .facebook .react .bridge .Callback ;
8+ import com .facebook .react .bridge .LifecycleEventListener ;
89import com .facebook .react .bridge .ReactApplicationContext ;
910import com .facebook .react .bridge .ReactContextBaseJavaModule ;
1011import com .facebook .react .bridge .ReactMethod ;
@@ -15,10 +16,49 @@ public CodePushDialog(ReactApplicationContext reactContext) {
1516 super (reactContext );
1617 }
1718
19+ private LifecycleEventListener mLifecycleEventListener = null ;
20+
1821 @ 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 );
2262
2363 builder .setCancelable (false );
2464
0 commit comments