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.
...
}
}