小编Kam*_*Bąk的帖子

Jetpack Compose 中的“@Stable”注释应用于函数/方法时会起什么作用?

我看到它被用于所有Density方法,这让我很好奇。

注释文档指出了这一点When applied to a function or a property, the Stable annotation indicates that the function will return the same result if the same parameters are passed in.,但这实际上会影响性能吗?

remember我尝试用下面的代码片段测试它(认为它会跳过/缓存函数的结果,就像在内部调用它一样)

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            var counter by remember {
                mutableStateOf(0)
            }
            val key = foo(counter > 0)
            LaunchedEffect(key){
                while (isActive){
                    delay(1000)
                    counter++
                }
            }
        }
    }
}

@Stable
fun foo(toReturn: Boolean): Boolean{
    Log.d("TEST", toReturn.toString())
    return toReturn
} …
Run Code Online (Sandbox Code Playgroud)

performance kotlin android-jetpack-compose

5
推荐指数
1
解决办法
568
查看次数