首先,基于android studio,先将虚拟手机的屏幕自动翻转打开
在Androidmanifests.xml文件中,设置 android:screenOrientation=“protrait"或"landscape” ,锁定屏幕竖直或水平。
create landscape variation
利用onCreate(Bundle savedInstanceState)方法里的savedInstanceState参数传递数据
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("KEY",textView.getText().toString()); //抓取TextView内容保存到outState变量中
}
if(savedInstanceState!=null){
textView.setText(savedInstanceState.getString("KEY"));
}
利用代码补全的onSaveInstanceState()是这样的
public void onSaveInstanceState(@NonNull Bundle outState, @NonNull PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
}
翻转后会丢失数据,改成protected方法并去掉后面那个参数就可以