0

So I have to create a simple highscore system, that will save the highscore somewhere in the system memory so that it won't reset every time I open the application.

For this, I guess the correct answer is to use the "Room" database. I've watched a lot of tutorials but I still didn't understand anything.

Here is what I want to do:

//my ints:
public class MainActivity extends AppCompatActivity {
int score = 0;
int highscore;
//and so on...
}

//load on app launch:
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //load from database

//save new highscore
private void save_highscore () {
if (highscore > score) {
    //save to database module 
    }
}
  • If you want to store it locally, you can use [SharedPreferences](https://stackoverflow.com/questions/23024831/android-shared-preferences-example). – Tepits Aug 01 '18 at 08:00
  • I believe this is as detailed for Room beginner as it gets: https://codelabs.developers.google.com/codelabs/android-room-with-a-view . – Sheler Aug 01 '18 at 08:01

1 Answers1

3

you can use SharedPreferences if you don't want to use Room.

save to:

SharedPreferences sp = getSharedPreferences("your_pref_key", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putInt("your_int_key", yourValue);
editor.commit();

load to:

SharedPreferences sp = getSharedPreferences("your_pref_key", Activity.MODE_PRIVATE);
 int myValue = sp.getInt("your_int_key", -1);

-1 is a default value.

secret paladin
  • 222
  • 1
  • 8
  • This is the best way if you just want to save the high score. No need for the extra complexity that comes with handling a database engine. – Ricardo Costeira Aug 01 '18 at 08:58