admin管理员组

文章数量:1287840

In this example, I just want to get the HTML from Google and show it in my Screen. I have a loading screen, until the response arrived, so the Screen can be updated. The problem is, that the UI Screen updates 10 seconds late, even though the correct HTTP Response came already immediately. Also the invalidate function is correctly called, but has no immediate effect on the UI update.

override fun onGetTemplate(): Template {
    if (currentTemplate != null) {
        return currentTemplate!!
    }

    val contentRow = Row.Builder()
        .setTitle(carContext.getString(R.string.loading))
        .build()

    val paneTemplate = PaneTemplate.Builder(
        Pane.Builder()
            .addRow(contentRow)
            .build()
    )
        .setHeaderAction(Action.BACK)
        .setTitle(carContext.getString(R.string.test))
        .build()

    currentTemplate = paneTemplate
    fetchPrivacyPolicyContent()

    return paneTemplate
}

private fun fetchContent() {
    CoroutineScope(Dispatchers.IO).launch {
        try {
            val response = RetrofitClient.apiService.getGoogle()
            if (response.isSuccessful) {
                val htmlContent = response.body() ?: ""
                withContext(Dispatchers.Main) {
                    val updatedRow = Row.Builder()
                        .setTitle(carContext.getString(R.string.test))
                        .addText(htmlContent)
                        .build()

                    val updatedPane = Pane.Builder()
                        .addRow(updatedRow)
                        .build()

                    currentTemplate = PaneTemplate.Builder(updatedPane)
                        .setHeaderAction(Action.BACK)
                        .setTitle(carContext.getString(R.string.test))
                        .build()

                    invalidate()
                    Log.e("ContentScreen", "TEST")

                }
            } 

... } }

val apiService: ApiService by lazy {
    Retrofit.Builder()
        .baseUrl(";)
        .addConverterFactory(ScalarsConverterFactory.create())
        .build()
        .create(ApiService::class.java)
}

Whatever I did, invalidate, push and pop the screen after response, nothing seems to work.

本文标签: Android Automotive Update UI Screen after CoroutineScope HTTP RequestStack Overflow