Skip to content

Commit 337a03e

Browse files
authored
Merge pull request #424 from Microsoft/add-resume-listener-immediate
Add resume listener for immediate installs
2 parents 8127584 + 3ce96e9 commit 337a03e

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

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

+23-10
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,11 @@ private void loadBundle() {
102102
mCodePush.clearDebugCacheIfNeeded();
103103
final Activity currentActivity = getCurrentActivity();
104104

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)) {
106110
// Our preferred reload logic relies on the user's Activity inheriting
107111
// from the core ReactActivity class, so if it doesn't, we fallback
108112
// early to our legacy behavior.
@@ -379,7 +383,12 @@ protected Void doInBackground(Void... params) {
379383
mSettingsManager.savePendingUpdate(pendingHash, /* isLoading */false);
380384
}
381385

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+
383392
// Store the minimum duration on the native module as an instance
384393
// variable instead of relying on a closure below, so that any
385394
// subsequent resume-based installs could override it.
@@ -392,15 +401,19 @@ protected Void doInBackground(Void... params) {
392401

393402
@Override
394403
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()) {
403405
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+
}
404417
}
405418
}
406419

0 commit comments

Comments
 (0)