Restrictions on starting activities from the background (original) (raw)

Android 10 (API level 29) and higher place restrictions on when apps can startactivities when the app runs in the background. These restrictions help minimize interruptions for the user and keep the user more in control of what's shown on their screen.

This guide presents notifications as an alternative for starting activities from the background. It also lists the specific cases where the restriction doesn't apply.

Display notifications instead

In nearly all cases, apps in the background mustdisplay time-sensitive notifications to provide urgent information to the user instead of directly starting an activity. Such notifications include handling an incoming phone call or an active alarm clock.

This notification-based alert and reminder system provides several advantages for users:

When apps can start activities

Apps running on Android 10 or higher can start activities when one or more of the following conditions are met:

Opt-In required when starting activities from PendingIntents

To avoid allowing accidental Activity starts based on the listed conditions, starting with Android 14 there are explicit APIs that allow you to opt in or out of granting an app permissions for Activity starts.

Apps targeting Android 15 or higher will default to no longer implicitly granting background activity launch (BAL) privileges to PendingIntents they create. Explicit opt-in is required, in order to do so, these are the options depending if the app is sending or creating PendingIntents.

Pending intents table

Figure 1: Decision flow for background activity launches.

By the Sender of the PendingIntent

Apps targeting Android 14 or higher that want to start a PendingIntent must

This opt-in should only happen if the app developer knows that the app is going to start an Activity.

To opt in, the app should pass an ActivityOptions bundle withsetPendingIntentBackgroundActivityStartMode(ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOWED)to the PendingIntent.send() or similar methods.

By the Creator of the PendingIntent

Apps targeting Android 15 or higher that create a PendingIntent must nowexplicitly opt in to allow background activity launch if they want thosePendingIntents to be startable under the listed conditions.

In most cases, the app starting the PendingIntent should be the one to opt in. However, if the creating app needs to grant these privileges:

To opt in, the app should pass an ActivityOptions bundle withsetPendingIntentCreatorBackgroundActivityStartMode (ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOWED) to thePendingIntent.getActivity() or similar methods.

Read the relevant reference documentation for further details:

Strict Mode

Starting with Android 16, the app developer can enable Strict mode to get notified when an activity launch is blocked (or at risk of getting blocked when the app's target SDK is raised).

Example code to enable from early in your Application, Activity, or other application component's Application.onCreate() method:

 override fun onCreate(savedInstanceState: Bundle?) {
     super.onCreate(savedInstanceState)
     StrictMode.setVmPolicy(
         StrictMode.VmPolicy.Builder()
         .detectBlockedBackgroundActivityLaunch()
         .penaltyLog()
         .build());
     )
 }

Read the Strict mode documentation for more details.