当屏幕方向更改时,我正在尝试保存和恢复布尔值字段。不管出于什么原因,在第一次方向更改期间,字段都正确地保存/恢复,但是当方向更改回初始状态时,调用onSaveInstanceState()
时布尔值是错误的。
根据this answer,我在清单中的相应活动标签上添加了android:configChanges="orientation"
。以下代码包含相关方法,为清楚起见对其进行了简化:
public class SignatureActivity extends AppCompactActivity {
private boolean signaturePadIsEmpty = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String orientation = getResources().getConfiguration().orientation == ORIENTATION_PORTRAIT ? "portrait" : "landscape";
Log.d(TAG, "onCreate: Orientation is " + orientation + ", signaturePadIsEmpty = " + signaturePadIsEmpty);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
signaturePadIsEmpty = savedInstanceState.getBoolean("SignaturePadIsEmpty");
String orientation = getResources().getConfiguration().orientation == ORIENTATION_PORTRAIT ? "portrait" : "landscape";
Log.d(TAG, "onRestoreInstanceState: Orientation is " + orientation + ", signaturePadIsEmpty = " + signaturePadIsEmpty);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("SignaturePadIsEmpty", signaturePadIsEmpty);
String orientation = getResources().getConfiguration().orientation == ORIENTATION_PORTRAIT ? "portrait" : "landscape";
Log.d(TAG, "onSaveInstanceState: Orientation is " + orientation + ", signaturePadIsEmpty = " + signaturePadIsEmpty);
}
}
这是我开始活动并从纵向->横向然后从横向->纵向更改时的Logcat输出:
06-27 11:50:08.037 32656-32656/com.example.www.debug D/SignatureActivity: onCreate: Orientation is portrait, signaturePadIsEmpty = true
06-27 11:50:12.405 32656-32656/com.example.www.debug D/SignatureActivity: onSaveInstanceState: Orientation is landscape, signaturePadIsEmpty = false
06-27 11:50:12.461 32656-32656/com.example.www.debug D/SignatureActivity: onCreate: Orientation is landscape, signaturePadIsEmpty = true
06-27 11:50:12.465 32656-32656/com.example.www.debug D/SignatureActivity: onRestoreInstanceState: Orientation is landscape, signaturePadIsEmpty = false
06-27 11:50:15.068 32656-32656/com.example.www.debug D/SignatureActivity: onSaveInstanceState: Orientation is portrait, signaturePadIsEmpty = true
06-27 11:50:15.131 32656-32656/com.example.www.debug D/SignatureActivity: onCreate: Orientation is portrait, signaturePadIsEmpty = true
06-27 11:50:15.133 32656-32656/com.example.www.debug D/SignatureActivity: onRestoreInstanceState: Orientation is portrait, signaturePadIsEmpty = true
编辑:该问题原来是由我对签名板的事件处理程序的实现引起的:
private void initializeLayoutElements(final int orientation) {
signaturePad = findViewById(R.id.signature_pad);
signaturePad.setOnSignedListener(new SignaturePad.OnSignedListener() {
@Override
public void onStartSigning() {
signaturePadIsEmpty = false;
}
@Override
public void onSigned() {
}
@Override
public void onClear() {
signaturePadIsEmpty = true;
}
});
}
onClear()
方法在onRestoreInstanceState()
之后被调用,并将标志设置为true
。由于@SOreadytohelp发布的解决方案回答了我最初的问题,所以我接受他们的回答。
答案 0 :(得分:1)
我在Github上创建了一个简单的演示,以向您展示如何存储和还原实例状态,如果您理解它,请告诉我。 这是一个具有按钮的简单应用程序,当您按下按钮时,可以更改布尔值的状态,并且如果旋转屏幕,您仍具有与以前相同的值。我希望这是您要寻找的。 p>
他们的密钥在onRestoreInstanceState()
中,得到Boolean
;在onSaveInstanceState()
中,保存Boolean
,而您在{{1 }}
android:configChanges="orientation