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

Commit cdbb8db

Browse files
committed
fix NPE when showing dialog while app is backgrounded
1 parent 47c792e commit cdbb8db

File tree

1 file changed

+43
-3
lines changed

1 file changed

+43
-3
lines changed

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

Lines changed: 43 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;
@@ -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

Comments
 (0)