Skip to content

CastContext never restores previous CastState after app returns to foreground #2925

@WSteverink

Description

@WSteverink

Version

Media3 main branch

More version details

Summary

CastContext stops updating its CastState after the app returns from the background. When the app is backgrounded, the state changes from DEVICE_AVAILABLE to NO_DEVICES_AVAILABLE as expected, but on resume the state never returns to DEVICE_AVAILABLE and no further state callbacks are triggered. This leaves the app stuck in an incorrect cast-state until restart.

We are updating our UI based on this CastState, which is now partially broken.

Reproduce

To reproduce this, use the latest Media3 main branch and run the demo-session variant.

Within the MainActivity, add:

    override fun onResume() {
        super.onResume()
        val snapshot = CastContext.getSharedInstance(this).castState
        Log.d("[DEBUG]", "onResume: snapshot $snapshot")
    }

And within the MainActivity.onCreate add:

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        CastContext.getSharedInstance(this).addCastStateListener { state ->
            Log.d("[DEBUG]", "addCastStateListener: $state <-----")
        }

Now run the app and make sure that casting devices are available.
Steps:

  1. Observe that the listener prints CastState 2 (DEVICES_AVAILABLE).
  2. Background the app.
  3. Observe that the listener prints CastState 1 (NO_DEVICES_AVAILABLE) — as expected.
  4. Return the app to the foreground.
  5. Observe that the listener does not print a new state, and the snapshot printed in onResume is still CastState 1 (NO_DEVICES_AVAILABLE).

I might be missing something, but it seems that this listener is not working correctly. Any help is highly appreciated. Keep up the good work, and thanks in advance.

Devices that reproduce the issue

Samsung a51 Android 13
Pixel 7 Android 16

Devices that do not reproduce the issue

No response

Reproducible in the demo app?

Yes

Bug Report

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions