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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![dlpuxdzztg](https://secure.gravatar.com/avatar/054f57454bab3cfb8ae9f9e405c18142?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
dlpuxdzztg
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](https://secure.gravatar.com/avatar/b5bdc43c1e576145fb404edba6fea5a6?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
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.