how to use getSharedPreferences in android

AndroidAndroid PreferencesSharedpreferences

Android Problem Overview


I have an application in which I have to implement a "Login" activity. I have these components:

  1. EditText username
  2. EditText password
  3. Button Login
  4. Button Cancel

I want that my application to remember the login details of the user once the user has logged in until the user would press the "log out" button. I'm not using preferences in my xml.

How do I get the getSharedPreferences(String name, int mode) to work in my application?

Android Solutions


Solution 1 - Android

First get the instance of SharedPreferences using

SharedPreferences userDetails = context.getSharedPreferences("userdetails", MODE_PRIVATE);

Now to save the values in the SharedPreferences

Editor edit = userDetails.edit();
edit.putString("username", username.getText().toString().trim());
edit.putString("password", password.getText().toString().trim());
edit.apply();

Above lines will write username and password to preference

Now to to retrieve saved values from preference, you can follow below lines of code

String userName = userDetails.getString("username", "");
String password = userDetails.getString("password", "");

(NOTE: SAVING PASSWORD IN THE APP IS NOT RECOMMENDED. YOU SHOULD EITHER ENCRYPT THE PASSWORD BEFORE SAVING OR SKIP THE SAVING THE PASSWORD)

Solution 2 - Android

//Set Preference
SharedPreferences myPrefs = getSharedPreferences("myPrefs", MODE_PRIVATE);
SharedPreferences.Editor prefsEditor;  
prefsEditor = myPrefs.edit();  
//strVersionName->Any value to be stored  
prefsEditor.putString("STOREDVALUE", strVersionName);  
prefsEditor.commit();

//Get Preferenece  
SharedPreferences myPrefs;    
myPrefs = getSharedPreferences("myPrefs", MODE_PRIVATE);  
String StoredValue=myPrefs.getString("STOREDVALUE", "");

Try this..

Solution 3 - Android

After reading around alot, only this worked: In class to set Shared preferences:

SharedPreferences userDetails = getApplicationContext().getSharedPreferences("test", MODE_PRIVATE);
SharedPreferences.Editor edit = userDetails.edit();
edit.clear();
edit.putString("test1", "1");
edit.putString("test2", "2");
edit.commit();

In AlarmReciever:

SharedPreferences userDetails = context.getSharedPreferences("test", Context.MODE_PRIVATE);
String test1 = userDetails.getString("test1", "");
String test2 = userDetails.getString("test2", "");

Solution 4 - Android

If someone used this:

val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)

PreferenceManager is now depricated, refactor to this:

val sharedPreferences = context.getSharedPreferences(context.packageName + "_preferences", Context.MODE_PRIVATE)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCMAView Question on Stackoverflow
Solution 1 - AndroidDharmendraView Answer on Stackoverflow
Solution 2 - AndroidKarthikView Answer on Stackoverflow
Solution 3 - AndroidAfshin GhaziView Answer on Stackoverflow
Solution 4 - AndroidnorbDEVView Answer on Stackoverflow