1

I need to access settings from various activities in my Android app. Rather than messing around with SharedPreferences in various activities I want to create a Singleton class, and grab all the settings at once using SharedPreferences in the Singleton. I also want a method in the Singleton to save settings where required, again using SharedPreferences.

The trouble with this is that I need an Activity context to use SharedPreferences. Could I pass this into the Singleton.. I've read about memory leaks and am wary of doing this.

Or am I completely barking up the wrong tree and is there a better way of sharing application settings between activities?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
vinnyh
  • 277
  • 3
  • 11

2 Answers2

3

You can try:

public class SomeApplication extends Application {


    private static SomeApplication _instance;

    public SomeApplication() {
       super();
    }

    public static SomeApplication getInstance() {
       return _instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();  
        _instance = this;
    }

    public SharedPreferences getPreferences() {
         return getApplicationContext().getSharedPreferences( SOME_PREFS_KEY, Context.MODE_PRIVATE );
    }

}

    .....
       SharedPreferences prefs =  SomeApplication.getInstance().getPreferences();
    ..... 

Don't forget about manifest.xml

    <application android:label="@string/app_name" android:icon="@drawable/icon"
             android:name="SomeApplication"
       >
Vyacheslav Shylkin
  • 9,741
  • 5
  • 39
  • 34
  • Thought about inheriting Application but my app is split between a main and library project - classes using the settings are in the library project and the Application class would be in the main project. So I can't easily reference anything created in the main project from the library project, if that makes sense? As you can tell I'm pretty new to this! – vinnyh Feb 27 '12 at 21:13
  • Ah but I suppose I could use context.GetApplication() in the library project to get it? – vinnyh Feb 27 '12 at 21:15
  • In that case you can use Singleton and pass into it not Activity context but activity.getApplicationContext() and not wary about memory leaks when your activity finished or recreated. – Vyacheslav Shylkin Feb 27 '12 at 21:28
  • I have a problem with this example because the `private void getPreferences()` is returning a value although it's a void. – Eugene van der Merwe Dec 29 '13 at 15:55
2

As far as sharing information between Activities, you pretty much have two options:

  1. Singleton Class, like you suggested.
  2. Extend the Application class and store your information there (which to my limited knowledge, is some sort of buffed Singleton).

Personally I use Singletons to manage Application Scoped Information in my Android applications. I'd like to give you the pros and cons to both approach myself but it's been debated heavily all over the internet ;) This question answered all my question when I got into Android programming and was trying to figure out how to shared information in my application in the most intelligent way possible.

I, personally never had any problems passing my Context to Singletons in order for them to do work related to Shared Preferences. I guess you've just have to be careful about what you're doing, like anything you're coding really.

Community
  • 1
  • 1
Jean-Philippe Roy
  • 4,752
  • 3
  • 27
  • 41
  • 1
    I do like the idea of Singletons as they seem a bit cleaner than Extending Application - my Singleton is called Settings so it's immediately obvious what it's doing. And it looks like passing Context is fine as long as you don't do anything like store it in a static which would leak it. Safest way looks to be passing this.GetApplicationContext() within an activity, and this is what I've implemented - works a treat. – vinnyh Feb 27 '12 at 22:40