Android SavedStateHandle doesn't save in ViewModel

杀马特。学长 韩版系。学妹 提交于 2020-06-17 02:00:35

问题


I have a ViewModel which takes a SavedStateHandle parameter. I am saving a String in there like this:

    private fun saveString(str: String) {
        state.set(KEY_STRING, str)
    }

Then I force-close my app and relaunch it, and want to retrieve the saved string like this:

fun getSavedString(): String? {
        return state.get<String>(KEY_String)
    }

However, it always returns null. Any ideas how to use SavedStateHandle correctly?

来源:https://stackoverflow.com/questions/62139420/android-savedstatehandle-doesnt-save-in-viewmodel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!