#I can't get my counter to continue counting from sharedpreferences

3 messages · Page 1 of 1 (latest)

viral flare
#

I've been able to save scores from previous sessions with sharedpreferences, however I haven't been able to resume counting from the last saved int

public class GameDisplay extends AppCompatActivity {

public int scoreX, xpCount;
private TextView Xcounter, xpcount;

@Override
    protected void onResume(){
        super.onResume();
        SharedPreferences sp = getSharedPreferences("MySharedPrefs", MODE_PRIVATE);
        int xscore = sp.getInt("Xcount", 0);
        int xpi = sp.getInt("xp", 0000);

        Xcounter.setText(String.valueOf(xscore));
        xpcount.setText(String.valueOf(xpi));

        scoreX = Integer.parseInt(String.valueOf(xscore));
        xpCount = Integer.parseInt(String.valueOf(xpi));
    }


private void performAction(ImageView imageView, int selectedBoxPosition){
if (checkPlayerWin()){
                scoreX++;
                Xcounter.setText(""+scoreX);
                xpCount++;
                xpcount.setText("000"+(5*xpCount))
            }else (totalSelectedBoxes == 9){}

@Override
    protected void onPause() {
        super.onPause();
        SharedPreferences sharedPreferences = getSharedPreferences("MySharedPref",MODE_PRIVATE);
        SharedPreferences.Editor myEdit = sharedPreferences.edit();
        myEdit.putInt("Xcount", Integer.parseInt(Xcounter.getText().toString()));
        myEdit.putInt("xp", Integer.parseInt(xpcount.getText().toString()));
        myEdit.apply();

    }
}

mellow tideBOT
#

This post has been reserved for your question.

Hey @viral flare! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.