Android SavedInstanceState - 如何从不同类型的方法保存变量

时间:2018-03-16 19:44:14

标签: java android methods android-savedstate onsaveinstancestate

我正在实施一个简单的应用程序(网球得分),我希望保存设置变量,然后在旋转屏幕时可以通过方法销毁它们:

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

}

我接下来要做的是能够保存剩余的(设置的变量),我不能这样做因为set的变量具有不同类型的方法(View)而我无法在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++;
        }

有没有办法找出这种问题? THX!

1 个答案:

答案 0 :(得分:0)

默认情况下,当屏幕旋转时,您的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);
         }

}