-3

i'm (trying) to create a soccer goals calculator, with two activities, MainActivity and Score, but i'd like to save int value that represent goals when clicking on back button, that what i tried:

public class Score extends Activity {
public Button plus;
public Button minus;
public TextView scoretext;
public int value;
public static final String mypreference = "mypref";
SharedPreferences sharedpreferences;
public void save(View v){
    SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putInt("inte", value); // Storing integer
    editor.apply(); // commit changes
}
public void restore(View v){
    this.sharedpreferences = this.getSharedPreferences("myPerf",
            Context.MODE_PRIVATE);

    if (this.sharedpreferences.contains("inte")) {
        okeh.setText(this.sharedpreferences.getInt("inte",value));
    }

}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_score);
    plus = this.findViewById(R.id.plus);
    scoretext=this.findViewById(R.id.scoretext);
    minus=this.findViewById(R.id.minus);

    plus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            value++;
            String scorestring=String.valueOf(value);
            scoretext.setText(scorestring);
        }
    });
    minus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            value--;
            String scorestring=String.valueOf(value);
            scoretext.setText(scorestring);
        }
    });
    sharedpreferences = this.getSharedPreferences(mypreference,
            Context.MODE_PRIVATE);
    if (sharedpreferences.contains("inte")) {
        okeh.setText(sharedpreferences.getInt("inte", value));
    }
}
}
Zoe
  • 27,060
  • 21
  • 118
  • 148
Yahya Chtaibi
  • 125
  • 1
  • 10

2 Answers2

1

You have to Override the onBackPressed method in your Score activity and save the int score in the shared preferences.

When you press the device back button the onBackPressed method will be called automatically and your latest Score will be saved in the shared preferences.

@Override
public void onBackPressed() {
    SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putInt("inte", value); // Storing integer
    editor.apply(); // commit changes

    super.onBackPressed();
}
Muazzam A.
  • 647
  • 5
  • 17
  • And what will happen if user switches to another application and Android removes it from memory? – Boris Treukhov Aug 26 '19 at 21:29
  • Then you can also override the **onUserLeaveHint** method as well and call onBackPressed in that function like this @Override protected void onUserLeaveHint() { onBackPressed(); super.onUserLeaveHint(); } – Muazzam A. Aug 26 '19 at 21:39
  • but how to settext when reopening the acitivity? – Yahya Chtaibi Aug 26 '19 at 21:43
  • call restore method in onResume of your activity like this. @Override protected void onResume() { SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("MyPref", Context.MODE_PRIVATE); if (this.sharedPreferences.contains("inte")) { okeh.setText(this.sharedPreferences.getInt("inte", value) + ""); } super.onResume(); } – Muazzam A. Aug 27 '19 at 18:59
1

Your save and restore methods have a parameter View v and i'm not sure why.

Change it to an integer

public void save(int value){
    sharedPreferences = getApplicationContext().getSharedPreferences("MyPref", Context.MODE_PRIVATE); // 0 - for private mode
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putInt("inte", value); // Storing integer
    editor.apply(); // commit changes
}

public int restore(){
    sharedPreferences = getApplicationContext().getSharedPreferences("MyPref", Context.MODE_PRIVATE);
    if (this.sharedPreferences.contains("inte")) {
        okeh.setText(this.sharedPreferences.getInt("inte",value));
    }
    return sharedPreferences.getInt("inte", 0); //return 0 if no data found
}

Alt + Insert and select override method

@Override
public void onBackPressed() {
    super.onBackPressed();
    // Put your sharedpreferences here that saves the int.
    save(value);
}

And now you can call your save method on places where you want to save the int value.

EDIT

plus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            value++;
            String scorestring=String.valueOf(value);
            scoretext.setText(scorestring);
            save(value);
        }
    });
    minus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            value--;
            String scorestring=String.valueOf(value);
            scoretext.setText(scorestring);
            save(value);
        }
    });
Vince VD
  • 1,506
  • 17
  • 38