kcNeko
kcNeko

Reputation: 609

How to save score to SharedPreferences then update it?

I have an app which shows the total earnings of stars in the MainActivity as textview. I want to do is input 0 if the app is run for the first time. Then afterwards as I finish a level and earn some stars (like currentScore + 50 = newScore) it will be redirected to MainActivity with the newScore textView.

This is my MainActivity where the the textview shows the score

public class MainActivity extends Activity {

  private static TextView txt_stars;

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

    public void updateStars() {
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putInt("Stars", 0);
    editor.commit();
    txt_stars = (TextView) findViewById(R.id.txtStars);
    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        //the key is "Stars" don't forget to type it as you created the Sp
    String stars = String.valueOf(sharedPreferences.getInt("Stars", 0));
    txt_stars.setText(stars);
}
}

And for the activity where I read the current score then add 50 to it then update

public void onClick(DialogInterface dialog, int whichButton) {

     SharedPreferences sharedPreferences = 
     PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
     Integer newstars = sharedPreferences.getInt("Stars", 0);
     Integer totalStars = newstars + 50;
     SharedPreferences.Editor editor = sharedPreferences.edit();
     editor.putInt("Stars", totalStars);
     editor.commit();

  //please help me improve my code here

    Intent nextForm = new Intent(.MainActivity");
    startActivity(nextForm);

  }

And also can a SharedPreferences be use in different activities? Like I just save a score from ActivityOne and can be read in ActivityTwo?

Thanks so much!

Upvotes: 1

Views: 100

Answers (2)

Skizo-ozᴉʞS ツ
Skizo-ozᴉʞS ツ

Reputation: 20656

Well, you should first read the SharedPreferences documentation, and then follow my steps to save values with SharedPreferences

Your UpdateStars() method should look like this :

public class updateStars() {
txt_stars = (TextView) findViewById(R.id.txtStars);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
//the key is "Stars" don't forget to type it as you created the Sp
String stars = String.valueOf(sharedPreferences.getInt("Stars", 0));
txt_stars.setText(stars);
}

And everytime you want to keep the users Stars you'll have to use the putInt() :

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("Stars", YOUR_INT_VALUE); //Here you save the stars of the user
editor.commit();

Upvotes: 1

Sebastian Breit
Sebastian Breit

Reputation: 6159

I once wrote a useful SharedPreferences class to write/retrieve data easily, which you can be copy/pasted from here:

https://github.com/asystat/bus_locator/blob/master/benasque2014/Autobus/src/com/example/com/benasque2014/mercurio/KeyStoreController.java

Then you write like this:

KeyStoreController.getKeyStore().setPreference("yourKey", yourValue)

yourValue can be int, boolean, long or string

and to retrieve:

KeyStoreController.getKeyStore().getInt("yourKey", defaultValue)
KeyStoreController.getKeyStore().getLong("yourKey", defaultValue)
KeyStoreController.getKeyStore().getString("yourKey", defaultValue)
KeyStoreController.getKeyStore().getBoolean("yourKey", defaultValue)

You can also call KeyStoreController.getKeyStore().appOpened() in the onCreate method of your MainApplication or Launcher Activity, and then you can use getTimesOpened() and isFirstLaunch() for any feature you'd like to add to your app.

I hope it can be useful to someone.

Upvotes: 1

Related Questions