gpt4 book ai didi

android - 实例状态未正确恢复/保存

转载 作者:行者123 更新时间:2023-11-29 00:53:51 26 4
gpt4 key购买 nike

我正在尝试在屏幕方向发生变化时保存和恢复 bool 字段。无论出于何种原因,该字段在第一次方向更改期间正确保存/恢复,但当方向更改回初始状态时,调用 onSaveInstanceState() 时 bool 值是错误的。

根据 this answer ,我将 android:configChanges="orientation" 添加到 list 中相应的 Activity 标签。以下代码包含相关方法,为清楚起见进行了简化:

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);
}
}

这是我开始 Activity 并从纵向 -> 横向然后从横向 -> 纵向更改时的 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 发布的解决方案回答了我最初的问题,我接受他们的回答。

最佳答案

我在 Github 上创建了一个简单的演示向您展示如何存储和恢复实例状态,如果您理解,请告诉我。这是一个简单的应用程序,有一个按钮,当您按下按钮时,您会更改 bool 值的状态,如果您旋转屏幕,您仍然拥有与以前相同的值。我希望是您正在寻找的东西。

它们的关键是在 onRestoreInstanceState() 中获取 Boolean 并在 onSaveInstanceState() 中保存 Boolean 和你不需要 manifest.xml

中的这个 android:configChanges="orientation
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
signaturePadIsEmpty = savedInstanceState.getBoolean("SignaturePadIsEmpty");
tvState.setText(String.valueOf(signaturePadIsEmpty));

}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("SignaturePadIsEmpty", signaturePadIsEmpty);
}

关于android - 实例状态未正确恢复/保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56795741/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com