简体   繁体   English

为什么用户输入没有通过 Android 10 上的 onRestoreInstanceState 恢复?

[英]Why user input isn't restored via onRestoreInstanceState on Android 10?

I have this following code:我有以下代码:

public class MainActivity extends AppCompatActivity {

    EditText edtInput;
    private static final String STATE_RESULT = "state_result";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        edtInput = (EditText) findViewById(R.id.edtInput);

    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        outState.putString(STATE_RESULT, edtInput.getText().toString());
        super.onSaveInstanceState(outState);

    }

    @Override
    protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        if (savedInstanceState != null){
            edtInput.setText(savedInstanceState.get(STATE_RESULT).toString());
        }
    }
}

On Pixel 3 (running Android 12): type something in the EditText, then do a combo of screen rotations, turning on/off the screen, press back to home and open the app again.在 Pixel 3(运行 Android 12)上:在 EditText 中输入一些内容,然后进行屏幕旋转组合,打开/关闭屏幕,按返回主页并再次打开应用程序。 the user input is still on EditText用户输入仍在 EditText

On Xiaomi Pocophone F1 (running Android 10) there's a slightly different behaviour: EditText is cleared if you press back and open the app again.在小米 Pocophone F1(运行 Android 10)上,行为略有不同:如果您按回并再次打开应用程序,则会清除 EditText。 but no problem as long as you only turn on/off or rotate the screen.但只要您只打开/关闭或旋转屏幕就没有问题。

Not sure if I misunderstood Android activity lifecycle, or this is a bug on the OS.不确定我是否误解了 Android 活动生命周期,或者这是操作系统上的错误。 Any help is appreciated.任何帮助表示赞赏。

Before you can start figuring out why it's happening, you need to figure out what exactly is happening.在您开始弄清楚它发生的原因之前,您需要弄清楚到底发生了什么。

You should add some logging to see exactly where it fails.您应该添加一些日志记录以查看失败的确切位置。 Maybe on the F1, the app gets killed before it calls onSaveInstanceState() .也许在 F1 上,应用在调用onSaveInstanceState()之前就被杀死了。 Or maybe onRestoreInstanceState() has savedInstanceState == null :或者也许onRestoreInstanceState()savedInstanceState == null

@Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        outState.putString(STATE_RESULT, edtInput.getText().toString());
        super.onSaveInstanceState(outState);
        Log.d("***", "Saved")
    }

@Override
    protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        Log.d("***", "Restoring: " + (savedInstanceState == null))
        if (savedInstanceState != null){
            edtInput.setText(savedInstanceState.get(STATE_RESULT).toString());
        }
    }

And then look at the logs to see what happens on the F1 when you exit the app using the back arrow, and then re-launch it然后查看日志以查看使用后退箭头退出应用程序时 F1 上发生的情况,然后重新启动它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM