This is my solution based on the GuidedStepFragment from leanback, that will block any focus on the view that is not a child of your layout.
Create a custom view and make it as a root layout for the second Fragment that is on the top:
import android.R.attr
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintLayout
class GuidedConstraintLayout @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr) {
private var mFocusOutStart = false
private var mFocusOutEnd = false
fun setFocusOutStart(focusOutStart: Boolean) {
mFocusOutStart = focusOutStart
}
fun setFocusOutEnd(focusOutEnd: Boolean) {
mFocusOutEnd = focusOutEnd
}
override fun focusSearch(focused: View?, direction: Int): View? {
val newFocus = super.focusSearch(focused, direction)
if (direction == FOCUS_LEFT || direction == FOCUS_RIGHT || direction == FOCUS_UP || direction == FOCUS_DOWN) {
if (isDescendant(this, newFocus)) {
return newFocus
}
if (if (layoutDirection == LAYOUT_DIRECTION_LTR) attr.direction == FOCUS_LEFT else attr.direction == FOCUS_RIGHT) {
if (!mFocusOutStart) {
return focused
}
} else {
if (!mFocusOutEnd) {
return focused
}
}
}
return newFocus
}
private fun isDescendant(parent: ViewGroup, child: View?): Boolean {
var localChild = child
while (localChild != null) {
if (localChild === parent) {
return true
}
val p = localChild.parent
if (p !is View) {
return false
}
localChild = p
}
return false
}
}