主题更改时 Android 数据丢失(浅色/深色模式)
Android:我使用 Kotlin
制作了一个简单的 Todo
列表应用程序。并且此应用的主题(浅色/深色)设置为系统默认主题MODE_NIGHT_FOLLOW_SYSTEM
。现在的问题是,当我将主题更改为 Dark
模式时(因为我的模拟器的主题之前设置为 Light
主题),所有添加的 Todo 项目都消失了,并且在我更改时相同再次进入Light
模式。然后我注意到我的 ArrayList 的大小变成了 0(空)。
以下是可以更好地理解它的屏幕截图:
灯光模式
https://i.sstatic .net/eWbzO.png
将系统主题更改为深色
深色模式
https://i.sstatic.net/B9iqg.png
当单击 saveButton: Textview
时,任务将添加到 list: ArrayList
class MainActivity : AppCompatActivity() {
var modified = false
private lateinit var listView: ListView
private lateinit var input: EditText
private lateinit var saveButton: TextView
private lateinit var cancelButton: TextView
private val list = ArrayList<String>()
private lateinit var listAdapter: CustomAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
listView = findViewById(R.id.listView)
input = findViewById(R.id.input)
saveButton = findViewById(R.id.saveBtn)
cancelButton = findViewById(R.id.cancelBtn)
listAdapter = CustomAdapter(this, list)
listView.adapter = listAdapter
val warningToast = Toast.makeText(this,
"The text may be empty or contains only spaces",
Toast.LENGTH_LONG)
saveButton.setOnClickListener {
val text = input.text.toString()
// if text is empty or only contains blank
if (text.isEmpty() || text.isBlank()) {
warningToast.show()
return@setOnClickListener
}
if (!modified) {
// output list size 0 after changing theme
Log.d("MY_DEBUG", "size fo list is : ${list.size}")
list.add(input.text.toString())
} else {
// ...
}
input.text.clear()
listAdapter.notifyDataSetChanged()
}
// ...
}
}
我认为这是因为当主题模式更改时,它会触发 uiMode
配置更改。因此,该活动会自动重新创建。即使更改主题后我也想保留添加的项目。有什么办法可以防止添加的物品丢失。谢谢你!
Android: I have made a simple Todo
list app using Kotlin
. And the theme (Light/Dark) of this app is set to system default theme MODE_NIGHT_FOLLOW_SYSTEM
. Now the problem is that when I changed theme to Dark
mode(because my emulator's theme was set to Light
theme previously), all added Todo items were gone and same when I changed to Light
mode again. Then I noticed the size of my ArrayList
became 0
(empty).
Here is screenshots to understand it better:
Light Mode
https://i.sstatic.net/eWbzO.png
after changing system theme to Dark
Dark Mode
https://i.sstatic.net/B9iqg.png
tasks are added to list: ArrayList
when saveButton: Textview
is clicked
class MainActivity : AppCompatActivity() {
var modified = false
private lateinit var listView: ListView
private lateinit var input: EditText
private lateinit var saveButton: TextView
private lateinit var cancelButton: TextView
private val list = ArrayList<String>()
private lateinit var listAdapter: CustomAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
listView = findViewById(R.id.listView)
input = findViewById(R.id.input)
saveButton = findViewById(R.id.saveBtn)
cancelButton = findViewById(R.id.cancelBtn)
listAdapter = CustomAdapter(this, list)
listView.adapter = listAdapter
val warningToast = Toast.makeText(this,
"The text may be empty or contains only spaces",
Toast.LENGTH_LONG)
saveButton.setOnClickListener {
val text = input.text.toString()
// if text is empty or only contains blank
if (text.isEmpty() || text.isBlank()) {
warningToast.show()
return@setOnClickListener
}
if (!modified) {
// output list size 0 after changing theme
Log.d("MY_DEBUG", "size fo list is : ${list.size}")
list.add(input.text.toString())
} else {
// ...
}
input.text.clear()
listAdapter.notifyDataSetChanged()
}
// ...
}
}
I thought that this is because it triggers a uiMode
configuration change when theme mode is changed. So, the activity is recreated automatically. I want to keep added items even after changing theme. Is there any way to prevent loss of added items. Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更改主题会重新创建您的活动并重新启动您的
列表
。这并不是因为主题本身的改变。您必须使用自己的序列化方法(例如 JSON)将数据持久保存在数据库或 SharedPreferences 实例中。
Changing theme recreates your activity and it reinitiates your
list
. It is not because of changing theme itself.You have to save your data persistently, in a database or in a SharedPreferences instance with your own serialization method (like JSON).