当我在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)
}
答案 0 :(得分:2)
根本原因::您覆盖了错误的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)
}