Android: Save variables and settings on rotation

后端 未结 2 893
情歌与酒
情歌与酒 2021-01-25 02:19

Is there a way to save variables that are being changed in the code, when the screen rotates? Thank you in advance

2条回答
  •  长发绾君心
    2021-01-25 02:48

    You can save values into the instanceState Bundle when stopping an activity and restoring it when you start it. Like this:

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        // Read values from the "savedInstanceState"
    }
    
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // Save the values you need into "outState"
        super.onSaveInstanceState(outState);
    }
    

提交回复
热议问题