[问题] 萤幕转向后资料全部清空

楼主: greenlinux (GreenLinux)   2022-07-15 00:40:02
大家好:
请教有关萤幕转向后, 资料不能保留的问题..
我是用 fragment 的设计, 在 fragment.jave 的 onCreateView() 中
宣告一些变量, Ex: public int test1, test2, 其经过下面function的计算后
For example:
public void Calsum()
{
for (int i=0; i<100; i++){
test1+=i;
test2+=2*i;
}
}
呈现在萤幕上(用 tableview 列出), 但萤幕转向后, test1 及 test2
又变成 0, 请问要怎样保留这些资料, 使得 转向后仍可保留.
谢谢大家..
作者: ae86357961 (Jasper.Yang)   2022-07-15 02:14:00
override onSaveInstanceState或者将资料存在viewModel
作者: chenx5 (ccchaha)   2022-08-17 07:35:00
因为它其实是重开新view
作者: asdkmm5050 (Mr xx)   2022-09-04 18:06:00
data binding
作者: testwhat   2022-10-22 13:36:00
可以宣告处理android:configChanges 参照https://developer.android.com/guide/topics/resources/runtime-changes

Links booklink

Contact Us: admin [ a t ] ucptt.com