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 trialdlpuxdzztg
8,243 PointsWhere should I define SharedPreferences?
In my app, I'm trying to get SharedPreferences to work. However, the settings are located in a fragment, but the settings defined in the fragment effect each activity. With that said, I'm not sue a to where I define my SharedPreferences object, in the fragment (because thats where you tweak the settings), or in my MainAcitvity (because, obviously, that's the first thing you see when you open the app, and SharedPreferences will be used in every activity)?
1 Answer
Seth Kroger
56,413 PointsTwo popular ways to do it are: 1) Open SharedPreferences when and where you need them. Your SharedPreferences should have particular names to access them so there's no need to keep it open in the settings activity/fragment. Another is to extend the Application class for your app and put getter/setter methods for your settings there.