简体   繁体   English

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

[英]Android SavedInstanceState - how to save variable from different type of method

I am implementing a simple app (tennis score), and I want to save the set variables before they can be destroyed by the methods when rotating the screen:我正在实现一个简单的应用程序(网球比分),我想保存设置的变量,然后才能在旋转屏幕时被方法破坏:

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

} }

What I want to do next is to be able to save the remaining (set's variables), I can't do this because the set's variables have the different type of methods (View) and i can't call them onCreate(Bundle savedInstanceState) method我接下来要做的是能够保存剩余的(集合的变量),我不能这样做,因为集合的变量具有不同类型的方法(视图),我不能调用它们 onCreate(Bundle savedInstanceState)方法

example:例子:

 /**
 * 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++;
        }

Is there a way to figure out this kind of problem?有没有办法解决这种问题? Thx!谢谢!

By default, when the screen is rotated your Activity is killed and restarted.默认情况下,当屏幕旋转时,您的 Activity 将被终止并重新启动。 To make sure no data is lost, you need to properly save and restore your data using the lifecycle methods.为了确保没有数据丢失,您需要使用生命周期方法正确保存和恢复数据。

This is how to save.这是如何保存。

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.

}

This is how to obtain.这就是获取方式。

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

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

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