当我在listView中键入内容并旋转后,listView返回null而不是我键入的文本。请您解释一下为什么会发生这种情况。

override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
        super.onRestoreInstanceState(savedInstanceState)
        Log.i("ToDoList","OnRestoreInstanceState was called")

        val listSave = savedInstanceState?.getStringArrayList("list")
        this.list.clear()
        this.list.add(listSave.toString())

        adapter = ArrayAdapter <String> (this, android.R.layout.simple_list_item_1,list)
        toDoList.adapter = adapter
    }
override fun onSaveInstanceState(outState: Bundle?, outPersistentState: PersistableBundle?) {
        super.onSaveInstanceState(outState, outPersistentState)
        Log.i("ToDoList","OnSaveInstanceState was called")
        outState?.putStringArrayList("list", list)
    }

最佳答案

根本原因:您覆盖了错误的onSaveInstanceState()方法。

解决方案:您必须覆盖onSaveInstanceState(Bundle)而不是onSaveInstanceState(Bundle, PersistableBundle)

更改您的代码

override fun onSaveInstanceState(outState: Bundle?, outPersistentState: PersistableBundle?) {
    super.onSaveInstanceState(outState, outPersistentState)
    Log.i("ToDoList","OnSaveInstanceState was called")
    outState?.putStringArrayList("list", list)
}




override fun onSaveInstanceState(outState: Bundle?) {
    super.onSaveInstanceState(outState)
    Log.i("ToDoList","OnSaveInstanceState was called")
    outState?.putStringArrayList("list", list)
}

关于java - 旋转后OnRestoreInstanceState返回null,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56066660/

10-09 23:41