Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Shared Preferences

Tags:

android

I have to share preferences using the sharedpreferences class in android and the preferences have to be shared between two activities. How shall I pass these preferences from one activity to another activity? Static variables can be used but they're not working for me.

//code for setting shared preferences SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit(); editor.putString("login_session_key",response.getLogin_Session_Key()); editor.putString("user_name", username.getText().toString()); editor.commit();  //code for getting shared preferences SharedPreferences settings = getSharedPreferences(SignIn.PREFS_NAME,                 Activity.MODE_PRIVATE); username = (TextView) findViewById(R.id.username); String uname = settings.getString("user_name", null); username.setText(uname); 
like image 556
Sultan Saadat Avatar asked Apr 20 '11 18:04

Sultan Saadat


People also ask

What is Android shared preference?

A SharedPreferences object points to a file containing key-value pairs and provides simple methods to read and write them. Each SharedPreferences file is managed by the framework and can be private or shared. This page shows you how to use the SharedPreferences APIs to store and retrieve simple values.

Where are shared preferences stored Android?

SharedPreferences are stored in an xml file in the app data folder, i.e. SharedPreferences added during runtime are not stored in the Eclipse project. The default shared preferences file would actually be: /data/data/<package>/shared_prefs/<package>_preferences. xml .

What can I use instead of shared preferences in Android?

Jetpack DataStore is a new and improved data storage solution aimed at replacing SharedPreferences. Built on Kotlin coroutines and Flow, DataStore provides two different implementations: Data is stored asynchronously, consistently, and transactionally, overcoming most of the drawbacks of SharedPreferences.

What is the shared preferences?

Shared Preferences is the way in which one can store and retrieve small amounts of primitive data as key/value pairs to a file on the device storage such as String, int, float, Boolean that make up your preferences in an XML file inside the app on the device storage.


1 Answers

You should either pass them to the activity via the intent call or you should read the ones you need in the new activity.

Create a helper class that handles all shared preferences calls for all your activities. Then instantiate an instance of it on any activity that needs to store/get a preference.

public class AppPreferences {      public static final String KEY_PREFS_SMS_BODY = "sms_body";      private static final String APP_SHARED_PREFS = AppPreferences.class.getSimpleName(); //  Name of the file -.xml      private SharedPreferences _sharedPrefs;      private Editor _prefsEditor;       public AppPreferences(Context context) {          this._sharedPrefs = context.getSharedPreferences(APP_SHARED_PREFS, Activity.MODE_PRIVATE);          this._prefsEditor = _sharedPrefs.edit();      }       public String getSmsBody() {          return _sharedPrefs.getString(KEY_PREFS_SMS_BODY, "");      }       public void saveSmsBody(String text) {          _prefsEditor.putString(KEY_PREFS_SMS_BODY, text);          _prefsEditor.commit();      } } 

Then in your activity ...

public class MyActivity extends Activity {      private AppPreferences _appPrefs;      public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         _appPrefs = new AppPreferences(getApplicationContext());         // ...     } } 

and

String someString = _appPrefs.getSmsBody(); 

or

_appPrefs.saveSmsBody(someString); 
like image 122
Bill Mote Avatar answered Oct 04 '22 07:10

Bill Mote