@@ -102,7 +102,11 @@ private void loadBundle() {
102
102
mCodePush .clearDebugCacheIfNeeded ();
103
103
final Activity currentActivity = getCurrentActivity ();
104
104
105
- if (!ReactActivity .class .isInstance (currentActivity )) {
105
+ if (currentActivity == null ) {
106
+ // The currentActivity can be null if it is backgrounded / destroyed, so we simply
107
+ // no-op to prevent any null pointer exceptions.
108
+ return ;
109
+ } else if (!ReactActivity .class .isInstance (currentActivity )) {
106
110
// Our preferred reload logic relies on the user's Activity inheriting
107
111
// from the core ReactActivity class, so if it doesn't, we fallback
108
112
// early to our legacy behavior.
@@ -379,7 +383,12 @@ protected Void doInBackground(Void... params) {
379
383
mSettingsManager .savePendingUpdate (pendingHash , /* isLoading */ false );
380
384
}
381
385
382
- if (installMode == CodePushInstallMode .ON_NEXT_RESUME .getValue ()) {
386
+ if (installMode == CodePushInstallMode .ON_NEXT_RESUME .getValue () ||
387
+ // We also add the resume listener if the installMode is IMMEDIATE, because
388
+ // if the current activity is backgrounded, we want to reload the bundle when
389
+ // it comes back into the foreground.
390
+ installMode == CodePushInstallMode .IMMEDIATE .getValue ()) {
391
+
383
392
// Store the minimum duration on the native module as an instance
384
393
// variable instead of relying on a closure below, so that any
385
394
// subsequent resume-based installs could override it.
@@ -392,15 +401,19 @@ protected Void doInBackground(Void... params) {
392
401
393
402
@ Override
394
403
public void onHostResume () {
395
- // Determine how long the app was in the background and ensure
396
- // that it meets the minimum duration amount of time.
397
- long durationInBackground = 0 ;
398
- if (lastPausedDate != null ) {
399
- durationInBackground = (new Date ().getTime () - lastPausedDate .getTime ()) / 1000 ;
400
- }
401
-
402
- if (durationInBackground >= CodePushNativeModule .this .mMinimumBackgroundDuration ) {
404
+ if (installMode == CodePushInstallMode .IMMEDIATE .getValue ()) {
403
405
loadBundle ();
406
+ } else {
407
+ // Determine how long the app was in the background and ensure
408
+ // that it meets the minimum duration amount of time.
409
+ long durationInBackground = 0 ;
410
+ if (lastPausedDate != null ) {
411
+ durationInBackground = (new Date ().getTime () - lastPausedDate .getTime ()) / 1000 ;
412
+ }
413
+
414
+ if (durationInBackground >= CodePushNativeModule .this .mMinimumBackgroundDuration ) {
415
+ loadBundle ();
416
+ }
404
417
}
405
418
}
406
419
0 commit comments