3

I have a ForegroundService that runs a large task. The user can press the home button or navigate away to a different app while the service runs. When the service completes or hits a "trigger" a new Intent is started. When the new Intent starts, I want the app to come back into the foreground (or focus) so that the user can continue with the app. Prior to API v30 (and I think v29), this worked great. I know that with API v29, there are new restrictions on starting activities from the background. One of the exceptions to the restriction is "The app receives a notification PendingIntent from the system. In the case of pending intents for services and broadcast receivers, the app can start activities for a few seconds after the pending intent is sent." I start the new Intent with a PendingIntent to try to take advantage of the restriction exception, but this only works on older API versions. Furthermore, I would assume that one of these two additional exceptions would apply: "The app has an activity in the back stack of the foreground task" or "The app has an activity in the back stack of an existing task on the Recents screen" but I don't understand why either apprently does not apply.

How can I bring the app back into the foreground or make this exception work on v29 and newer?

Following the DEBUG Log messages in the below code, I only get to the "onCreate in NewIntent" log message in onCreate within NewIntent.kt after I manually bring the app back into focus when using API v30. Older API version automatically bring the app back into focus and jump right into the onCreate function in NewIntent.kt.

ForegroundService.kt
...
//When "trigger" is reached, start the new Intent and bring app to foreground if it was moved to background
private fun startNewActivity() {
    //First two lines are to shut down the foreground service since it has reached a trigger and is complete.
    super.onDestroy()
    stopSelf()
    
    //Start the new intent
    val startNewIntent = Intent(this, NewIntent::class.java)
    val pendingIntent = PendingIntent.getActivity(this, 0, startNewIntent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_ONE_SHOT)
    pendingIntent.send()
    Log.d("DEBUG", "PendingIntent sent") //App will hang here with new API version until app is manually brought back into foreground
}
...
NewIntent.kt

class NewIntent : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        ...
        Log.d("DEBUG", "onCreate in NewIntent") //Only can get here when app is manually brought back into foreground with new API version. 
        ...
    }
}
Tom
  • 717
  • 2
  • 6
  • 23
  • 1
    "How can I bring the app back into the foreground or make this exception work on v29 and newer?" -- in general, you can't. Use a high-priority notification with a full-screen `Intent`. "I start the new Intent with a PendingIntent to try to take advantage of the restriction exception" -- you missed the key part of the exception: "The app receives a **notification** PendingIntent from the system". – CommonsWare Nov 25 '20 at 00:17
  • Thanks for the info @CommonsWare. Do you know of a tutorial or example code for a full screen notification? – Tom Nov 25 '20 at 00:29
  • There is [this one from one of my books](https://github.com/commonsguy/cw-omnibus/tree/v9.0/Notifications/FullScreen), though it is a bit older and in Java. And there is [this one from the documentation](https://developer.android.com/training/notify-user/time-sensitive#create-high-priority-notification). – CommonsWare Nov 25 '20 at 00:43
  • Thanks @CommonsWare. There are other exceptions that sound like they should apply, but apparently they don't. This further confuses my understand as to why the original code does not work. Specifically, why do these two exceptions not apply: "The app has an activity in the back stack of the foreground task" or "The app has an activity in the back stack of an existing task on the Recents screen"? – Tom Nov 26 '20 at 01:06
  • "The app has an activity in the back stack of the foreground task" -- that exception would not appear to match your situation ("The user can press the home button or navigate away to a different app while the service runs"). "The app has an activity in the back stack of an existing task on the Recents screen" -- this appears to be covered by the note below that bullet ("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.") – CommonsWare Nov 26 '20 at 13:19
  • Could you do this? – Vadym Mar 25 '22 at 22:37

0 Answers0