diff --git a/CodePush.m b/CodePush.m index 481aa9f..cb6767b 100644 --- a/CodePush.m +++ b/CodePush.m @@ -6,7 +6,9 @@ #import "CodePush.h" -@implementation CodePush +@implementation CodePush { + BOOL hasRestartListener; +} RCT_EXPORT_MODULE() @@ -358,14 +360,17 @@ RCT_EXPORT_METHOD(installUpdate:(NSDictionary*)updatePackage rollbackTimeout:rollbackTimeout]; if (installMode == CodePushInstallModeImmediate) { - [self restartPendingUpdate]; + [self loadBundle]; } else if (installMode == CodePushInstallModeOnNextResume) { - // Register for app resume notifications so that we - // can check for pending updates which support "restart on resume" - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(restartPendingUpdate) - name:UIApplicationWillEnterForegroundNotification - object:[UIApplication sharedApplication]]; + if (!hasRestartListener) { + // Register for app resume notifications so that we + // can check for pending updates which support "restart on resume" + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(loadBundle) + name:UIApplicationWillEnterForegroundNotification + object:[UIApplication sharedApplication]]; + hasRestartListener = true; + } } // Signal to JS that the update has been applied. resolve(nil); diff --git a/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java b/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java index 6a2ec7f..915dcc1 100644 --- a/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java +++ b/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java @@ -1,6 +1,7 @@ package com.microsoft.codepush.react; import com.facebook.react.ReactPackage; +import com.facebook.react.bridge.ActivityEventListener; import com.facebook.react.bridge.JavaScriptModule; import com.facebook.react.bridge.LifecycleEventListener; import com.facebook.react.bridge.NativeModule; @@ -254,6 +255,8 @@ public class CodePush { private class CodePushNativeModule extends ReactContextBaseJavaModule { + private LifecycleEventListener lifecycleEventListener = null; + private void loadBundle() { Intent intent = mainActivity.getIntent(); mainActivity.finish(); @@ -273,22 +276,26 @@ public class CodePush { } if (installMode != CodePushInstallMode.IMMEDIATE.getValue()) { - restartPendingUpdate(); + loadBundle(); } else if (installMode == CodePushInstallMode.ON_NEXT_RESUME.getValue()) { - getReactApplicationContext().addLifecycleEventListener(new LifecycleEventListener() { - @Override - public void onHostResume() { - restartPendingUpdate(); - } + // Ensure we do not add the listener twice. + if (lifecycleEventListener == null) { + lifecycleEventListener = new LifecycleEventListener() { + @Override + public void onHostResume() { + loadBundle(); + } - @Override - public void onHostPause() { - } + @Override + public void onHostPause() { + } - @Override - public void onHostDestroy() { - } - }); + @Override + public void onHostDestroy() { + } + }; + getReactApplicationContext().addLifecycleEventListener(lifecycleEventListener); + } } promise.resolve("");