【问题标题】:How to support Map<String, Boolean> for rememberSaveable?如何支持 Map<String, Boolean> for rememberSaveable?
【发布时间】:2022-07-19 17:53:10
【问题描述】:

使用下面的

var state by rememberSaveable {
    mutableStateOf(
        enumValues<NavigationItem>().associate { it.route to true }
    )
}

enum class NavigationItem(val route: String, val icon: Int) {
    Home("home", R.drawable.ic_home),
    Music("music", R.drawable.ic_music),
    Movies("movies", R.drawable.ic_movie),
    Books("books", R.drawable.ic_book),
    Profile("profile", R.drawable.ic_profile)
}

rememberSaveable 没有保存和恢复状态。

如何保存和恢复?

【问题讨论】:

    标签: android android-jetpack-compose


    【解决方案1】:

    看起来确实可以保存。但是它有一些错误并且没有以正确的顺序恢复。所以制作我们自己的保护程序会有所帮助

    var state by rememberSaveable(
        saver = mapSaver(
            save = { it.value },
            restore = { mutableStateOf(it as Map<String, Boolean>) }
        )
    ) {
        mutableStateOf(enumValues<NavigationItem>().associate {
            it.route to true
        })
    }
    

    【讨论】:

      猜你喜欢
      • 2011-04-07
      • 2011-11-24
      • 1970-01-01
      • 1970-01-01
      • 2013-10-31
      • 1970-01-01
      • 2018-10-24
      • 1970-01-01
      • 2013-07-30
      相关资源
      最近更新 更多