@@ -168,7 +168,9 @@ protected void onResume() {
168168 isTargetActivityStarted = true ;
169169 PendingIntent startTargetActivityIntent =
170170 checkNotNull (getIntent ().getParcelableExtra (TARGET_ACTIVITY_INTENT_KEY ));
171- Bundle options = getIntent ().getBundleExtra (TARGET_ACTIVITY_OPTIONS_BUNDLE_KEY );
171+ Bundle options =
172+ optInToGrantBalPrivileges (
173+ getIntent ().getBundleExtra (TARGET_ACTIVITY_OPTIONS_BUNDLE_KEY ));
172174 try {
173175 if (options == null ) {
174176 // Override and disable FLAG_ACTIVITY_NEW_TASK flag by flagsMask and flagsValue.
@@ -441,15 +443,7 @@ public void startActivityForResult(Intent intent, @Nullable Bundle activityOptio
441443
442444 activityResultWaiter = new ActivityResultWaiter (getApplicationContext ());
443445
444- if (VERSION .SDK_INT >= VERSION_CODES .UPSIDE_DOWN_CAKE ) {
445- ActivityOptions activityOptions =
446- ActivityOptions .makeBasic ()
447- .setPendingIntentBackgroundActivityStartMode (MODE_BACKGROUND_ACTIVITY_START_ALLOWED );
448- if (activityOptionsBundle == null ) {
449- activityOptionsBundle = new Bundle ();
450- }
451- activityOptionsBundle .putAll (activityOptions .toBundle ());
452- }
446+ activityOptionsBundle = optInToGrantBalPrivileges (activityOptionsBundle );
453447
454448 // Note: Instrumentation.startActivitySync(Intent) cannot be used here because BootstrapActivity
455449 // may start in different process. Also, we use PendingIntent because the target activity may
@@ -470,6 +464,22 @@ public void startActivityForResult(Intent intent, @Nullable Bundle activityOptio
470464 getApplicationContext ().startActivity (bootstrapIntent , activityOptionsBundle );
471465 }
472466
467+ private static Bundle optInToGrantBalPrivileges (Bundle activityOptionsBundle ) {
468+ if (VERSION .SDK_INT < VERSION_CODES .UPSIDE_DOWN_CAKE ) {
469+ return activityOptionsBundle ;
470+ }
471+ // Initialize a bundle to grant this activities start privilege.
472+ Bundle updatedActivityOptions =
473+ ActivityOptions .makeBasic ()
474+ .setPendingIntentBackgroundActivityStartMode (MODE_BACKGROUND_ACTIVITY_START_ALLOWED )
475+ .toBundle ();
476+ // Merge the bundle with the one passed in. This allows overriding the start mode if desired.
477+ if (activityOptionsBundle != null ) {
478+ updatedActivityOptions .putAll (activityOptionsBundle );
479+ }
480+ return updatedActivityOptions ;
481+ }
482+
473483 @ Override
474484 public void startActivityForResult (Intent intent ) {
475485 startActivityForResult (intent , null );
0 commit comments