Why does this code work only with the Android Emulator but not with real devices?

Hi there,

I have the following problem: This code works with the Android Emulator but not with real devices:

            switchContext("NATIVE_APP")

            // Click on 'Allow' - permission
            val permissionsButton = timAndroidFinders.permissionsButton()
            permissionsButton.click()

            // Check if we are already in "Downloads"
            val documentsuiToolbarTitle = timAndroidFinders.documentsuiToolbarTitle()

            if (documentsuiToolbarTitle.text != "Downloads") {
.
.
.
    private val wait = WebDriverWait(driver, Duration.ofSeconds(20))

    private inline fun waitFor(call: () -> By): WebElement {
        val elementView = call()
        wait.until(ExpectedConditions.visibilityOfElementLocated(elementView))

        return driver.findElement(elementView)
    }


    fun permissionsButton(): WebElement {
        return waitFor { By.id("com.android.permissioncontroller:id/permission_allow_button") }
    }

    fun documentsuiToolbarTitle(): WebElement {
        return waitFor { By.xpath("//android.view.ViewGroup[@resource-id='com.android.documentsui:id/toolbar']/child::android.widget.TextView") }
    }

while the search and click on the permissions button works in both cases (Android Emulator and real device), the subsequent search for the documentsui toolbar’s title works only in the emulator, it times out on the real devices. The search xpath seems to be correct, given it works on the emulator and also after cross checking it in UI Automator Viewer:

Does anybody know what I am doing wrong here or what I am missing?

Thanks in advance,

Lars