Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to make SharedPreferences global throughout my whole Android app?

Is there a way to make SharedPreferences global throughout my whole app? Right now I'm using these lines in a lot of places throughout my code to store simple on/off settings for a number of preferences that my users can set. I just want to call them once globally if possible:

SharedPreferences settings = getSharedPreferences("prefs", 0);
SharedPreferences.Editor editor = settings.edit();

Any tips on how to be able to call just these line in all classes would be awesome:

editor.putString("examplesetting", "off");
editor.commit(); 

and

String checkedSetting = settings.getString("examplesetting", "");
like image 873
Ethan Allen Avatar asked Jul 19 '12 01:07

Ethan Allen


People also ask

Where are Android shared preferences stored?

Android stores Shared Preferences settings as XML file in shared_prefs folder under DATA/data/{application package} directory. The DATA folder can be obtained by calling Environment. getDataDirectory() .

Is jetpack DataStore a replacement for SharedPreferences?

DataStore is a new and improved data storage solution aimed at replacing SharedPreferences. Built on Kotlin coroutines and Flow, DataStore provides two different implementations: Proto DataStore, that stores typed objects (backed by protocol buffers) and Preferences DataStore, that stores key-value pairs.

What is the maximum number of shared preferences you can create?

there is no limit in Shared Preference. Save this answer.

Is shared preferences deprecated?

Yes, it is deprecated. Use the AndroidX Preference Library for consistent behavior across all devices. For more information on using the AndroidX Preference Library see Settings.


2 Answers

I know, I know, I will get inflamed, and casted into the embers of hell....

Use a singleton class that wraps around the SharedPreference settings.. something like this:

public class PrefSingleton{
   private static PrefSingleton mInstance;
   private Context mContext;
   //
   private SharedPreferences mMyPreferences;

   private PrefSingleton(){ }

   public static PrefSingleton getInstance(){
       if (mInstance == null) mInstance = new PrefSingleton();
       return mInstance;
   }

   public void Initialize(Context ctxt){
       mContext = ctxt;
       //
       mMyPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
   }
}

And create wrapper functions around what your examples represented in the question, for example,

PrefSingleton.getInstance().writePreference("exampleSetting", "off");

and the implementation could be something like this:

// Within Singleton class

public void writePreference(String key, String value){
     Editor e = mMyPreference.edit();
     e.putString(key, value);
     e.commit();
}

From your first activity, activate the singleton class, in this manner, something like this:

PrefSingleton.getInstance().Initialize(getApplicationContext());

The reason I risk down-vote, is, using global static classes can be a bad idea and goes against the practice of programming fundamentals. BUT having said that, as nit-picky aside, it will ensure only the one and only object of the class PrefSingleton can exist and be accessible regardless of what activities the code is at.

like image 120
t0mm13b Avatar answered Oct 21 '22 03:10

t0mm13b


I would extend Application and include the SharedPreferences.Editor as a field with a getter.

public class APP extends Application {
    private final SharedPreferences settings = getSharedPreferences("prefs", 0);
    private final SharedPreferences.Editor editor = settings.edit();

    public SharedPreferences.Editor editSharePrefs() {
        return editor;
    }
}

Then you can access it from any Activity with

((APP) getApplication()).editSharePrefs().putString("examplesetting", "off");
((APP) getApplication()).editsharePrefs().commit();

Alternatively, you could also throw in the method

    public static APP getAPP(Context context) {
        return (APP) context.getApplicationContext();
    }

Although, this would simply change the calls you make to

APP.getAPP(this).editSharePrefs().putString("examplesetting", "off");
APP.getAPP(this).editsharePrefs().commit();

So it really is a personal preference, which looks cleaner to you.

like image 21
gobernador Avatar answered Oct 21 '22 02:10

gobernador