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);
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.
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 .
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With