Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Android Data Persistence Key-Value Saving with SharedPreferences Reading a Value from SharedPreferences

What should be "int" parameter in getInt() method?

As can be seen, the getInt(String key, int defValue) method takes two parameters. The 'Key' parameter has been explicitly provided, but what value should I use for 'int' parameter?

PS: I used '0' as a workaround and the answer was accepted.

CodeChallenge.java
SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);

int answer = mSharedPreferences.getInt("MeaningOfLife", 0);

1 Answer

Ricky Catron
Ricky Catron
13,023 Points

I believe the int value can be anything. I think this creates a value pair which associates "MeaningOfLife" with 0.

MeaningOfLife in my opinion should be associated with 42. ;)

Goodluck! --Ricky

Thanks.