Live Activity Persistence After App Force Quit

I have a state in my app that is connected to a Live Activity. When the state is true, the Live Activity is active, and when it is false, the Live Activity is ended.

I have noticed, though, that if a user starts the Live Activity (by changing the state to true), then force quits the app, then reopens the app and changes the state to false, the Live Activity is not ended / removed from the lock screen.

Is there any way to fix this? Since there is no problem ending the Live Activity if the user doesn't force quit the app, it seems as though the Live Activity may be somehow connected to the specific instance of the app. Is there a way to not have the Live Activity connected to the only active instance of the app, but be able to have it ended by any future instance of the app?

Answered by Frameworks Engineer in 752926022

Can you help clarify a few things?

I have a state in my app that is connected to a Live Activity.

How is this state connected to the Live Activity?

...that if a user starts the Live Activity (by changing the state to true)...

How is the activity started when the user changes this state to true?

But to answer the question of Live Activities being tied to an instance of an app. Force quitting the app doesn't end the Live Activities. When the user relaunches the app, the app should be able to get all ongoing Live Activities via the Activity.activities variable: https://developer.apple.com/documentation/activitykit/activity/activities

Accepted Answer

Can you help clarify a few things?

I have a state in my app that is connected to a Live Activity.

How is this state connected to the Live Activity?

...that if a user starts the Live Activity (by changing the state to true)...

How is the activity started when the user changes this state to true?

But to answer the question of Live Activities being tied to an instance of an app. Force quitting the app doesn't end the Live Activities. When the user relaunches the app, the app should be able to get all ongoing Live Activities via the Activity.activities variable: https://developer.apple.com/documentation/activitykit/activity/activities

Thank you for the response.

There is a button in the app that triggers the start of a live activity. I'm not too familiar with how it works (or should work), but this is the code that triggers the activity:

activity = try! Activity<live_activity_name>.request(attributes: attributes, contentState: state, pushType: nil)

Then, when the user clicks the button again, it should end the live activity:

await activity?.end(using: state, dismissalPolicy: .immediate)

The problem is that when the user force quits the app, then reopens the app and triggers the code above to end the live activity, it does not actually end it. The lines of code listed above are within an if-else branch, with the condition being the value of the boolean state (related to whether the live activity should be active or ended).

Based on your response, does this mean that I should fetch all ongoing Live Activities using Activity.activities (instead of relying on the boolean variable within the app)?

Live Activity Persistence After App Force Quit
 
 
Q