One of the most Interesting Data Storage options Android provides its users is Shared PreferencesShared 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. Shared Preferences can be thought of as a dictionary or a key/value pair. For example, you might have a key being “username” and for the value, you might store the user’s username. And then you could retrieve that by its key (here username). You can have a simple shared preference API that you can use to store preferences and pull them back as and when needed. Shared Preferences class provides APIs for reading, writing, and managing this data. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language. 


Following are the methods of Shared Preferences 

  1. contains(String key): This method is used to check whether the preferences contain a preference. 
     
  2. edit(): This method is used to create a new Editor for these preferences, through which you can make modifications to the data in the preferences and atomically commit those changes back to the SharedPreferences object. 
     
  3. getAll(): This method is used to retrieve all values from the preferences. 
     
  4. getBoolean(String key, boolean defValue): This method is used to retrieve a boolean value from the preferences. 
     
  5. getFloat(String key, float defValue): This method is used to retrieve a float value from the preferences. 
     
  6. getInt(String key, int defValue): This method is used to retrieve an int value from the preferences. 
     
  7. getLong(String key, long defValue): This method is used to retrieve a long value from the preferences. 
     
  8. getString(String key, String defValue): This method is used to retrieve a String value from the preferences. 
     
  9. getStringSet(String key, Set defValues): This method is used to retrieve a set of String values from the preferences. 
     
  10. registerOnSharedPreferencechangeListener(SharedPreferences.OnSharedPreferencechangeListener listener): This method is used to registers a callback to be invoked when a change happens to a preference. 
     
  11. unregisterOnSharedPreferencechangeListener(SharedPreferences.OnSharedPreferencechangeListener listener): This method is used to unregisters a previous callback. 
Shared Preferences Example


public class MainActivity2 extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
btn=findViewById(R.id.btn);

ReadPref();
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharePref("Demo@gmail.com","12345");
}
});
}


void SharePref(String email,String Password)
{
SharedPreferences sharedPreferences=getSharedPreferences("Android",MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString("Email",email);
editor.putString("Password",Password);
editor.commit();
}

void ReadPref()
{
SharedPreferences sharedPreferences=getSharedPreferences("Android",MODE_PRIVATE);
String data = sharedPreferences.getString("Email",null);
String data1 = sharedPreferences.getString("Password",null);

Toast.makeText(MainActivity2.this, data + " "+data1, Toast.LENGTH_SHORT).show();
}





Show Data of mobile in Android Studio : -



Reference: Shared Preferences | Android