【发布时间】:2020-09-29 16:21:45
【问题描述】:
在屏幕旋转时,我希望保存 var cardcount,这样用户在旋转屏幕时不会重置为第一张闪存卡。
我尝试使用 onSaveInstanceState 和 savedInstanceState.getInt() 来保持旋转时的值,但它不保存值。
我已经看到了这些功能的替代品,但我不确定我是否会为了简单的事情而进入一些深兔子洞,或者我是否完全在错误的海洋中游泳。
我应该研究 ViewModel 和持久性存储、通过配置更改手动接管还是共享首选项?数据是一个变量,在一个片段中。
class Components : Fragment() {
//Variables to keep track of which card, as well as which side is showing
private var cardcount = 0
private var cardfront = true
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//This is the function to set the image and text for the front and back of the flashard
fun select_card() {
card_front.setImageResource(map.getValue(cardcount).image)
card_back.text = map.getValue(cardcount).text
}
}
【问题讨论】:
标签: android kotlin configuration bundles