Search code examples
android

Opening App From Background when App is in Recent Screen stack is not working


I have a code in place which triggers a deep link from a service running in the background. It works always that the ACTION_MANAGE_OVERLAY_PERMISSION permission is granted. This makes sense given the exceptions exposed in the docs. The issue is that in the documentation it also states that if the app is in the recent screens stack it should be possible to open it from background:

The app has an activity in the back stack of an existing task on the Recents screen.

I'm trying with a pretty straightforward piece of code from a service:

val currentIntent = Intent(this, MainActivity::class.java).apply {
    addCategory(Intent.CATEGORY_BROWSABLE)
    addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
}

startActivity(currentIntent)

And even though the app is in the recent screen stack, it won't open it unless the permission is granted.

Any help is appreciated.


Solution

  • In your circumstances, your activity will be queued up to open once that task is selected again:

    When such an app attempts to start a new activity, the system places that activity on top of the app's existing task but doesn't navigate away from the currently visible task. When the user later returns to the app's task, the system starts the new activity instead of the activity that had previously been on top of the app's task.

    (emphasis added)

    I guess this is middle ground between interrupting the user (which Android is now geared to avoid) and not including this approach in the allowlist for background activity launches.