Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit baf8234

Browse files
authored
Merge pull request #441 from Microsoft/fix-dialog-NPE
Fix NPE caused by showing dialog while app is backgrounded
2 parents 47c792e + 516f5ca commit baf8234

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

android/app/src/main/java/com/microsoft/codepush/react/CodePushDialog.java

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.content.DialogInterface;
66

77
import com.facebook.react.bridge.Callback;
8+
import com.facebook.react.bridge.LifecycleEventListener;
89
import com.facebook.react.bridge.ReactApplicationContext;
910
import com.facebook.react.bridge.ReactContextBaseJavaModule;
1011
import 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

Comments
 (0)