I want to use CastStateListener in a fragment to check if casting devices are available or not. Code used in Fragment is
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mCastContext = CastContext.getSharedInstance(mContext)
mCastStateListener = CastStateListener { newState ->
if (newState != CastState.NO_DEVICES_AVAILABLE) {
castDevicesAvailable = true
}
}
}
override fun onResume() {
super.onResume()
mCastContext?.addCastStateListener(mCastStateListener)
}
override fun onPause() {
super.onPause()
mCastContext?.removeCastStateListener(mCastStateListener)
}
This code does not give me a call back inside CastListner when used in Fragment but it works fine when I use it in an Activity or Fragment.
I am using custom view
<androidx.mediarouter.app.MediaRouteButton
android:id="@+id/media_route_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:actionButtonStyle="@android:style/Widget.Holo.Light.MediaRouteButton"/>
I want to hide/show the view based on cast devices available