应用关闭时覆盖共享首选项

时间:2016-01-07 12:26:46

标签: java android sharedpreferences

我的应用程序遇到了一个问题,即在强制关闭后重新打开应用程序时,共享首选项数据中的数据会被覆盖。

这是我的代码,我在这里开始app +我的日志调用

private String TAG = "MainActivity";
public int score;
public int highScore;
SharedPreferences data;
public static String filename = "HighScore"; // This is shared preference name

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    data = getSharedPreferences(filename, 0);
    Log.d(TAG, "Checking highscore Value Oncreate:" + highScore);
    Log.d(TAG, "Checking score Value Oncreate:" + score);
    Log.d(TAG, "Checking Shared preference highscore Value Oncreate:" + data.getInt("HighScore", 0));
}

这是我的方法,其中包括共享首选项的保存和调用功能。

 public void generateH(View v){
    Random rand = new Random();
    int number = rand.nextInt(2)+1;
    TextView myText = (TextView)findViewById(R.id.coinResult);

    if (number == 1){
        myText.setText("HEADS");
        TextView myScore = (TextView)findViewById(R.id.scoreTxt);
        score = score+1;
        String points = String.valueOf(score);
        myScore.setText(points);

        if(highScore > score)
        {
            // This will store the new high score in the sharedpreferences.
            SharedPreferences.Editor editor = data.edit();
            editor.putInt("HighScore", highScore);
            editor.apply(); // Use editor.apply() for saving in background

        }
        else
        {
            highScore = score;
            SharedPreferences.Editor editor = data.edit();
            editor.putInt("HighScore", score);
            editor.apply();
        }
    }

    else{
        myText.setText("TAILS");

        AlertDialog.Builder myAlert = new AlertDialog.Builder(this);
        myAlert.setTitle("You have lost");
        myAlert.setMessage("Your score was :" + score + "\n" + "Your Highscore is: " +  data.getInt("HighScore", 0) )
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .create();

        score = 0;
        TextView myScore = (TextView)findViewById(R.id.scoreTxt);
        String points = String.valueOf(score);
        myScore.setText(points);

        if(highScore > score)
        {
            // This will store the new high score in the sharedpreferences.
            SharedPreferences.Editor editor = data.edit();
            editor.putInt("HighScore", highScore);
            editor.apply(); // Use editor.apply() for saving in background
            // after this highscore will be 100
        }
        else
        {
            highScore = score;
            SharedPreferences.Editor editor = data.edit();
            editor.putInt("HighScore", score);
            editor.apply();
        }
        Log.d(TAG, "Checking highscore Value genHLoseCondition:" + highScore);
        Log.d(TAG, "Checking score Value genHLoseCondition::" + score);
        Log.d(TAG, "Checking Shared preference highscore Value genHLoseCondition::" + data.getInt("HighScore", 0));
        myAlert.show();
    }

}

以下是我的日志:

  

01-07 12:16:13.602 3189-3189 /? D / MainActivity:检查高分值Oncreate:0

     

01-07 12:16:13.602 3189-3189 /? D / MainActivity:检查得分值Oncreate:0

     

01-07 12:16:13.603 3189-3189 /? D / MainActivity:检查共享首选项高分值Oncreate:0

     

01-07 12:16:23.947 3189-3189 /? D / MainActivity:检查高分值genHLoseCondition:2

     

01-07 12:16:23.947 3189-3189 /? D / MainActivity:检查得分值genHLoseCondition :: 0

     

01-07 12:16:23.948 3189-3189 /? D / MainActivity:检查共享首选项高分值genHLoseCondition :: 2

     

01-07 12:16:46.446 3218-3218 /? D / MainActivity:检查高分值Oncreate:0

     

01-07 12:16:46.446 3218-3218 /? D / MainActivity:检查得分值Oncreate:0

     

01-07 12:16:46.460 3218-3218 /? D / MainActivity:检查共享首选项高分值Oncreate:2

     

01-07 12:16:48.624 3218-3218 /? D / MainActivity:检查高分值genHLoseCondition:0

     

01-07 12:16:48.624 3218-3218 /? D / MainActivity:检查得分值genHLoseCondition :: 0

     

01-07 12:16:48.624 3218-3218 /? D / MainActivity:检查共享首选项高分值genHLoseCondition :: 0

可以看出共享偏好值是在onCreate上记住的,但是在创建新值的那一刻,它会被传递到共享首选项中。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

在比较高分的if条件中,高分变量中的值为0,因为您没有为该变量分配共享首选项值。