From 20370fa3cac68e3c2e2c7b9780f435cb16867cf6 Mon Sep 17 00:00:00 2001 From: Geoffrey Goh Date: Thu, 28 Jul 2016 10:52:29 -0700 Subject: [PATCH] fix NPE when showing dialog while app is backgrounded --- .../com/microsoft/codepush/react/CodePushDialog.java | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/android/app/src/main/java/com/microsoft/codepush/react/CodePushDialog.java b/android/app/src/main/java/com/microsoft/codepush/react/CodePushDialog.java index 411852a..8187658 100644 --- a/android/app/src/main/java/com/microsoft/codepush/react/CodePushDialog.java +++ b/android/app/src/main/java/com/microsoft/codepush/react/CodePushDialog.java @@ -16,8 +16,6 @@ public class CodePushDialog extends ReactContextBaseJavaModule{ super(reactContext); } - private LifecycleEventListener mLifecycleEventListener = null; - @ReactMethod public void showDialog(final String title, final String message, final String button1Text, final String button2Text, final Callback successCallback, Callback errorCallback) { @@ -25,7 +23,7 @@ public class CodePushDialog extends ReactContextBaseJavaModule{ if (currentActivity == null) { // If getCurrentActivity is null, it could be because the app is backgrounded, // so we show the dialog when the app resumes) - mLifecycleEventListener = new LifecycleEventListener() { + getReactApplicationContext().addLifecycleEventListener(new LifecycleEventListener() { private boolean shown = false; @Override @@ -33,10 +31,7 @@ public class CodePushDialog extends ReactContextBaseJavaModule{ Activity currentActivity = getCurrentActivity(); if (!shown && currentActivity != null) { shown = true; - - // Set to null to allow GC. - mLifecycleEventListener = null; - + getReactApplicationContext().removeLifecycleEventListener(this); showDialogInternal(title, message, button1Text, button2Text, successCallback, currentActivity); } } @@ -50,7 +45,7 @@ public class CodePushDialog extends ReactContextBaseJavaModule{ public void onHostDestroy() { } - }; + }); } else { showDialogInternal(title, message, button1Text, button2Text, successCallback, currentActivity); }