Skip to content

Commit 802bcbd

Browse files
Merge pull request #167 from MihaiCristianCondrea/codex/fix-activityresultlauncher-registration-issue
Prevent unregistered in-app update launcher crash
2 parents 269fb9e + aa1826d commit 802bcbd

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

app/src/main/java/com/d4rk/androidtutorials/java/ui/screens/main/MainActivity.java

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import androidx.appcompat.app.AppCompatActivity;
2121
import androidx.core.splashscreen.SplashScreen;
2222
import androidx.lifecycle.DefaultLifecycleObserver;
23+
import androidx.lifecycle.Lifecycle;
2324
import androidx.lifecycle.LifecycleOwner;
2425
import androidx.lifecycle.ViewModelProvider;
2526
import androidx.navigation.NavController;
@@ -62,15 +63,7 @@
6263
public class MainActivity extends AppCompatActivity {
6364

6465
private static final long BACK_PRESS_INTERVAL = 2000;
65-
private final ActivityResultLauncher<IntentSenderRequest> updateActivityResultLauncher =
66-
registerForActivityResult(
67-
new ActivityResultContracts.StartIntentSenderForResult(),
68-
result -> {
69-
if (result.getResultCode() != Activity.RESULT_OK) {
70-
Log.d("MainActivity", "In-app update flow failed! " + result.getResultCode());
71-
}
72-
}
73-
);
66+
private ActivityResultLauncher<IntentSenderRequest> updateActivityResultLauncher;
7467
private final SparseIntArray navOrder = new SparseIntArray();
7568
private ActivityMainBinding mBinding;
7669
private final DefaultLifecycleObserver lifecycleObserver = new DefaultLifecycleObserver() {
@@ -101,6 +94,15 @@ public void onResume(@NonNull LifecycleOwner owner) {
10194
protected void onCreate(Bundle savedInstanceState) {
10295
SplashScreen.installSplashScreen(this);
10396
super.onCreate(savedInstanceState);
97+
98+
updateActivityResultLauncher = registerForActivityResult(
99+
new ActivityResultContracts.StartIntentSenderForResult(),
100+
result -> {
101+
if (result.getResultCode() != Activity.RESULT_OK) {
102+
Log.d("MainActivity", "In-app update flow failed! " + result.getResultCode());
103+
}
104+
}
105+
);
104106
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
105107
if (!prefs.getBoolean(getString(R.string.key_onboarding_complete), false)) {
106108
startActivity(new Intent(this, StartupActivity.class));
@@ -315,21 +317,24 @@ protected void onResume() {
315317
}
316318

317319
private void checkForFlexibleOrImmediateUpdate() {
318-
appUpdateManager.getAppUpdateInfo().addOnSuccessListener(appUpdateInfo -> {
319-
boolean updateAvailable = appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE;
320-
if (updateAvailable) {
320+
appUpdateManager
321+
.getAppUpdateInfo()
322+
.addOnSuccessListener(this, appUpdateInfo -> {
323+
boolean updateAvailable =
324+
appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE;
325+
if (updateAvailable && getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) {
321326
startImmediateUpdate(appUpdateInfo);
322327
}
323328
})
324-
.addOnFailureListener(e -> {
329+
.addOnFailureListener(this, e -> {
325330
if (!BuildConfig.DEBUG) {
326331
Snackbar.make(
327332
findViewById(android.R.id.content),
328333
getString(R.string.snack_general_error),
329334
Snackbar.LENGTH_LONG
330335
).show();
331336
}
332-
});
337+
});
333338
}
334339

335340
private void checkInAppReview() {
@@ -348,6 +353,11 @@ private void checkInAppReview() {
348353
}
349354

350355
private void startImmediateUpdate(AppUpdateInfo appUpdateInfo) {
356+
if (updateActivityResultLauncher == null
357+
|| !getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) {
358+
Log.w("MainActivity", "Update launcher not ready");
359+
return;
360+
}
351361
try {
352362
appUpdateManager.startUpdateFlowForResult(
353363
appUpdateInfo,

0 commit comments

Comments
 (0)