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 ;
@@ -16,9 +17,40 @@ public CodePushDialog(ReactApplicationContext reactContext) {
1617 }
1718
1819 @ 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 );
2254
2355 builder .setCancelable (false );
2456
0 commit comments