Detecting a lost render context
As of v11.16.0, you can detect a lost render context withRiveWorker.renderContextStatus. This StateFlow starts as RenderContextStatus.Ready and changes to RenderContextStatus.Lost when Rive detects a loss. It never changes back.
Rive detects a loss the next time the worker draws, and updates the status while the worker is polled. rememberRiveWorker and rememberRiveWorkerOrNull poll the worker for you by default while the lifecycle is resumed. If you create a RiveWorker yourself, you need to call beginPolling.
Ready only means that Rive hasn’t detected a loss. It doesn’t mean every operation on the worker will succeed, so keep handling the errors that Rive APIs can throw. Rive can also miss some losses, such as one that first shows up while the surface it draws to is being created or resized.
Recovering with a new worker
To recover, create a new worker and load your content on it again. If you create workers with theRiveWorker constructor, release the lost worker yourself. Also close anything you loaded from it without a remember function, such as files and images. The old worker shuts down only after it’s released and everything loaded from it is closed.
Recovering in Compose
See the Compose render context recovery example, where an AndroidViewModel owns the worker.
You can put the worker and everything you load from it inside a key block, and change the key when the status changes to Lost. When the key changes, Compose disposes the block. The remember functions in it close what they loaded and release the old worker. The block then runs again with a new worker.
Creating the new worker can fail, so the example below uses rememberRiveWorkerOrNull. It returns null instead of throwing, which lets you show a fallback.
Recovering with RiveCanvasSession
The experimental RiveCanvasSession stops advancing and rendering after a loss, but beginPlaying doesn’t return until you close the session or cancel its coroutine. Watch renderContextStatus yourself, then close the session and what you loaded, and create them again on a new worker. See the Canvas render context recovery example.