今日总结:项目活动被摧毁时,保存上次的数据 (android stdio)
屏幕竖放转换为横放时
1.使用 onSaveInstanceState函数 --(不是永久保存)
protected void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
outState.putString(“KEY”,...);
}
将...存入outState参数中,然后数据即为在onCreate(Bundle savedInstanceState)的参数中,使用此参数即为实现数据的保存。
2.使用ViewModel 保存数据,在切换其他程序时,数据依然存在
创建一个继承ViewModel父类的类,将主活动中的数据保存在创建类中的数据变量中。

浙公网安备 33010602011771号