1

我正在实现一个简单的应用程序(网球得分),我想保存设置的变量,然后在旋转屏幕时被方法破坏:

public class MainActivity extends AppCompatActivity {
int scorePlayerOne = 0;    // Tracks the score for Player One
int scorePlayerTwo = 0;    // Tracks the score for Player Two
int set1PlayerOne = 0;     // Track the score set for Player One
int set2PlayerOne = 0;
int set3PlayerOne = 0;
int set1PlayerTwo = 0;     // Track the score set for Player Two
int set2PlayerTwo = 0;
int set3PlayerTwo = 0;
int setOfTheGame = 1;      // Tracks the current set of the game

// Declaring and initializing KEY_INDEX to serve as NVP (Name-Value Pair).
private static final String KEY_INDEX = "index";
private static final String KEY_INDEX2 = "index2";

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (savedInstanceState != null) {
        scorePlayerOne = savedInstanceState.getInt(KEY_INDEX, scorePlayerOne);
    }
    if (savedInstanceState != null) {
        scorePlayerTwo = savedInstanceState.getInt(KEY_INDEX2, scorePlayerTwo);
    }
    setContentView(R.layout.activity_main);
    displayForPlayerOne(scorePlayerOne);
    displayForPlayerTwo(scorePlayerTwo);
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
      /*
    *Save UI state changes to the savedInstanceState.
    * This bundle will be passed to onCreate if the process is
    * killed and restarted.
    * Storing a NVP ("Name-Value Pair") map, and it will get
    * passed in to onCreate () method.
    */
    savedInstanceState.putInt(KEY_INDEX, scorePlayerOne);
    savedInstanceState.putInt(KEY_INDEX2, scorePlayerTwo);
}

}

我接下来要做的是能够保存剩余的(集合的变量),我不能这样做,因为集合的变量具有不同类型的方法(视图),我不能调用它们 onCreate(Bundle savedInstanceState)方法

例子:

 /**
 * Increase the score of the set for Player One by 1 point.
 */
public void addSetScoreForPlayerOne(View v) {

    if (setOfTheGame == 1) {
        if ((set1PlayerOne == 6 && set1PlayerOne - set1PlayerTwo >= 2)
                || (set1PlayerOne == 7)) {
            setOfTheGame = 2;
            set2PlayerOne++;
            TextView scoreView = findViewById(R.id.player_one_score_set_2);
            scoreView.setText(String.valueOf(set2PlayerOne));
        } else {
            set1PlayerOne++;
        }

有没有办法解决这种问题?谢谢!

4

1 回答 1

2

默认情况下,当屏幕旋转时,您的 Activity 会被终止并重新启动。为确保没有数据丢失,您需要使用生命周期方法正确保存和恢复数据。

这是如何保存。

protected void onSaveInstanceState (Bundle outState) {

    super.onSaveInstanceState(outState);
    outState.putCharSequence(EDIT_TEXT_VALUE, mTextView.getText()); //<-- Saving operation, change the values to what ever you want.

}

这是如何获得的。

protected void onCreate(Bundle savedInstanceState) {
    
    super.onCreate(savedInstanceState);
    mTextView = (TextView) findViewById(R.id.editText);
    
    // Retrieving logic
    if (savedInstanceState != null) {
        CharSequence savedText = savedInstanceState.getCharSequence(EDIT_TEXT_VALUE);
        mTextView.setText(savedText);
    }
    
}
于 2018-03-16T20:07:46.287 回答