Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


José Eduardo Eguiguren
José Eduardo Eguiguren
17,555 Points

Save a high score for an Android game even after closing the activity or app

I need to store a value somehow permanently to keep the player's highscore. I've seen people used SharedPreferences:

SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putInt("key", score);

However I do not understand exactly how to store the value there and after doing so, how am i supposed to get it back when I need it?

Thank you in advance.

I see you have the input part , the "key" is essentially the name of the variable you are passing into the next screen and score is its value. I used this in mine intent.putExtra("types",types); .

And I used this to recieve my values in the next screen. I sent in an arraylist of strings in my example, and wanted to put the list in a function as I called it a few times. This is that code.

public static Intent aop;

public static ArrayList<String> gettypes() { return (ArrayList<String>) aop.getExtras().get( "types"); }

As you can see we recieve our values with the variable name we assigned before. Hope this helps.

José Eduardo Eguiguren
José Eduardo Eguiguren
17,555 Points

So it works just like passing data with an intent, with the key name representing the variable?

Now once that I store it I just get it later with the method you put in just calling the value with the key right?

Thanks Ashley.

Yup you got it.

No problem.