oziomajnr
oziomajnr

Reputation: 1741

Android Chrome Cast Introduction Overlay has incorrect behaviour when fragment has options menu

I am showing the Cast button as an Options menu item that is inflated from an activity, but I noticed that when the activity has a child fragment and the child fragment does not have an options menu item by itself, the chrome cast introduction overlay works correctly. However when the fragment has its own options menu, the Cast introduction overlay does not work correctly, it either shows in the top left corner or shows up in the correct position but does not highlight the cast icon. enter image description here

Here is the code to initialize the Overlay

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        loadCastButton(menu)
        return super.onCreateOptionsMenu(menu)
    }

private fun loadCastButton(menu: Menu?) {
    menuInflater.inflate(R.menu.menu_cast, menu)
    CastButtonFactory.setUpMediaRouteButton(applicationContext, menu, R.id.cast_menu_item)
    val mediaRoutebutton = menu?.findItem(R.id.cast_menu_item)?.actionView as? MediaRouteButton
    mediaRoutebutton?.dialogFactory = CastDialogFactory()
    handleCastTutorial(menu)
}

private fun handleCastTutorial(menu: Menu?) {
    val castButton = menu?.findItem(R.id.cast_menu_item)
    if (castButton == null) {
        return
    }
    castViewModel.isCastingAvailable.observe(this) {
        if (it == true && castButton.isVisible) {
            //Show cast tutorial
                castViewModel.setCastTutorialShown(true)
                IntroductoryOverlay.Builder(this, castButton)
                        .setTitleText(R.string.cast_tutorial_title)
                        .setSingleTime()
                        .build()
                        .show()

        }
    }
}

Upvotes: 2

Views: 292

Answers (1)

Daniel Zolnai
Daniel Zolnai

Reputation: 16910

When you are showing Cast buttons in fragments and activities, menus are inflated everywhere, with Cast buttons initialized in one of the fragments or activities and then immediately hidden again. My recommended solution is delaying the cast tutorial with a minor amount of delay, and then checking for visibility and window attach status again:

if (!castViewModel.getCastTutorialShown()) {
    binding.root.postDelayed(200L) {
    // Check if it is still visible.
    if (castButton.isVisible && castButton.actionView.isAttachedToWindow && !castViewModel.getCastTutorialShown()) {
        castViewModel.setCastTutorialShown(true)
        IntroductoryOverlay.Builder(this, castButton)
            .setTitleText(R.string.cast_tutorial_title)
            .setSingleTime()
            .build()
            .show()
        }
    }
}

Upvotes: 1

Related Questions