如何使用“onsaveinstancestate”?

eqfvzcg8  于 2021-07-12  发布在  Java
关注(0)|答案(2)|浏览(368)

我想达到什么目的?

我想保存 x , y 以及 score ,因此当用户旋转屏幕时,将检索值而不是重新生成。

我有什么?

可能是对的 onSaveInstanceState 代码不起作用可能是因为 @Override 与之发生冲突

我需要什么?

一些修正可以帮助我保存变量的值

代码

mainactivity.java(无 onSaveInstanceState )

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    private EditText textBox;
    private int z;
    public int score = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ((TextView) findViewById(R.id.scoreText)).setText(R.string.nullScore);
        textBox = findViewById(R.id.editText);
        textBox.requestFocus();
        setActual();
    }

    private void setActual() {
        Random random = new Random();
        int x = random.nextInt(10) + 1; //you get a random number between 0 and 9 then add 1 to get 1 - 10, no need to use array..
        int y = random.nextInt(10) + 1;
        z = x * y;
        String hintStr = x + " × " + y;
        ((TextView) findViewById(R.id.hintTextView)).setText(hintStr);
    }

            public void onCheck(View view) {

        int answer = Integer.parseInt(textBox.getText().toString());
        String res = "Incorrect";
        if (answer == z) {
            res = "Correct";
            score++;
            Log.d("TAG", "Correct."); // right answer
            textBox.setText("");
            setActual(); // let's regenerate values
        } else if (score == 0) {
            Log.d("TAG", "Incorrect."); // wrong answer
            textBox.setText("");
            setActual();
            ((TextView) findViewById(R.id.scoreText)).setText(R.string.nullScore);
        } else {
            Log.d("TAG", "Incorrect."); // wrong answer
            textBox.setText("");
            score--;
            setActual();
        }
        ((TextView) findViewById(R.id.scoreText)).setText("Score:" + ' ' + score);
        ((TextView) findViewById(R.id.result_text_view)).setText(res);
    }

}

mainactivity.java(带 onSaveInstanceState )

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    private EditText textBox;
    private int z;
    public int score = 0;
    private int x;
    private int y;

    public void onSaveInstanceState(Bundle savedInstanceState) {
        savedInstanceState.putInt("x", x);
        savedInstanceState.putInt("y", y);
        savedInstanceState.putInt("score", score);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textBox = findViewById(R.id.editText);
        textBox.requestFocus();
        setActual();
        if (savedInstanceState != null) {
            x = savedInstanceState.getInt("x");
            y = savedInstanceState.getInt("y");
            score = savedInstanceState.getInt("score");
        }
    }

    private void setActual() {
        Random random = new Random();
        x = random.nextInt(10) + 1; //you get a random number between 0 and 9 then add 1 to get 1 - 10, no need to use array..
        y = random.nextInt(10) + 1;
        z = x * y;
        String hintStr = x + " × " + y;
        ((TextView) findViewById(R.id.hintTextView)).setText(hintStr);
    }

            public void onCheck(View view) {

        int answer = Integer.parseInt(textBox.getText().toString());
        String res = "Incorrect";
        if (answer == z) {
            res = "Correct";
            score++;
            Log.d("TAG", "Correct."); // right answer
            textBox.setText("");
            setActual(); // let's regenerate values
        } else if (score == 0) {
            Log.d("TAG", "Incorrect."); // wrong answer
            textBox.setText("");
            setActual();
            ((TextView) findViewById(R.id.scoreText)).setText(R.string.nullScore);
        } else {
            Log.d("TAG", "Incorrect."); // wrong answer
            textBox.setText("");
            score--;
            setActual();
        }
        ((TextView) findViewById(R.id.scoreText)).setText("Score:" + ' ' + score);
        ((TextView) findViewById(R.id.result_text_view)).setText(res);
    }

}
balp4ylt

balp4ylt1#

您应该考虑调用super.onsaveinstancestate()

@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
    // save your state
    super.onSaveInstanceState(outState);
}

另外,为了避免每次旋转屏幕时都重新创建活动,您可以通过将此添加到应用程序清单中的活动声明中来处理配置更改:

android:configChanges="keyboardHidden|orientation|screenSize"

和覆盖:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
}

它可以为用户节省时间,如果你的应用程序不需要重新创建后,方向或屏幕大小的变化。。。

ogq8wdun

ogq8wdun2#

是否确定未检索到值?我认为这里的问题是,您总是在oncreate()中调用setactual(),它生成x和y值,并将z值赋给生成的x和y(即使是savedinstancestate!=(空)
处理事情的更好方法是

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textBox = findViewById(R.id.editText);
    textBox.requestFocus();

    if (savedInstanceState != null) {
        x = savedInstanceState.getInt("x");
        y = savedInstanceState.getInt("y");
        score = savedInstanceState.getInt("score");
        z = x * y;
        String hintStr = x + " × " + y;
        ((TextView) findViewById(R.id.hintTextView)).setText(hintStr);
    } else { setActual(); }
}

您还应该这样调用super.onsaveinstancestate(savedinstancestate)

@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putInt("x", x);
    savedInstanceState.putInt("y", y);
    savedInstanceState.putInt("score", score);
}

相关问题