【问题标题】:What is the exact meaning of 'emit' in Android Jetpack Compose?Android Jetpack Compose 中“发射”的确切含义是什么?
【发布时间】:2021-08-18 07:34:56
【问题描述】:

emit这个词经常出现在Jetpack Compose的文档或codelabs中,如下:

该函数不返回任何内容。 “发出” UI 的 Compose 函数不需要返回任何内容,因为它们描述了所需的屏幕状态,而不是构建 UI 小部件。

Android Jetpack Compose 中emit的确切含义是什么?

谁处理 Compose 函数发出的 UI? Compose 框架是否检测并处理发出的 UI?

是否有文档说明如何以及由谁处理发出的 UI?

【问题讨论】:

  • 我相信这里提供的答案会有所帮助。 How compose works under the hood
  • @javatar 这不是问题的直接答案,但感谢您提供了解 Compose 基础知识的好文章 :) 我试图通过观看随附的文章和视频 Understanding Compose (Android Dev Summit '19) 来理解。
  • 只是暗示它把它渲染出来。如果有任何处理程序,它们将是操作系统的内部层。

标签: android android-jetpack android-jetpack-compose


【解决方案1】:

“Emit”表示 Compose 将一个新组插入到当前组合中。

source code

@Suppress("NONREADONLY_CALL_IN_READONLY_COMPOSABLE", "UnnecessaryLambdaCreation")
@Composable inline fun <T : Any, reified E : Applier<*>> ReusableComposeNode(
    noinline factory: () -> T,
    update: @DisallowComposableCalls Updater<T>.() -> Unit
) {
    if (currentComposer.applier !is E) invalidApplier()
    currentComposer.startReusableNode()   // <--- EMITTING THE NODE
    if (currentComposer.inserting) {
        currentComposer.createNode { factory() }
    } else {
        currentComposer.useNode()
    }
    currentComposer.disableReusing()
    Updater<T>(currentComposer).update()
    currentComposer.enableReusing()
    currentComposer.endNode()
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-10
    • 2011-12-14
    • 1970-01-01
    • 1970-01-01
    • 2017-04-19
    • 2011-05-06
    • 2013-07-22
    相关资源
    最近更新 更多