In this article, will see what are SharedPreferences and how to use them into your Android Project. SharedPreferences are XML files to store private primitive data in key-value pairs and provides you with read and write methods to access that data. SharedPreferences data is accessible throughout the application and are stored even after the application closes.
SharedPreferences includes data types such as Boolean, Float, Int, Long and String. You can create SharedPreferences which can be private or public i.e private means they will be accessed only by your application and public means any other apps that know the file identifier can access your data.
Now we will see how to implement SharedPreferences step-by-step:
Creating a new shared preference file or access an existing one. This can be done by two methods.
1. getSharedPreferences() : Using this methods you can create Multiple SharedPreferences and its first parameters in name of SharedPreferences.
Context context = getActivity(); SharedPreferences sharedPref = context.getSharedPreferences("FILE_NAME", Context.MODE_PRIVATE);
2. getPreferences() : Using this method you can create Single SharedPreferences.
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
*Note: You can create SharedPreferences file with MODE_WORLD_READABLE or MODE_WORLD_WRITEABLE. By this mode any other apps that know the file identifier can access your data.
To write data into SharedPreferences file, You need to create SharedPreferences.Editor by calling the edit() method on your SharedPreferences.
After that you can add data with the help of Key-value pairs into SharedPreferences. Following methods can be used to add data as per the respective data types i.e putBoolean(), putInt(), putFloat(), putLong() and putString().
Finally to store the data or to save the changes, you need to call commit() or apply() method.
*Note: apply() is asynchronous call to perform disk I/O where as commit() is synchronous.
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPref.edit(); editor.putBoolean("key_name", true); // Saving boolean value editor.putInt("key_name", "int value"); // Saving integer value editor.putFloat("key_name", "float value"); // Saving float value editor.putLong("key_name", "long value"); // Saving long value editor.putString("key_name", "string value"); // Saving string value editor.commit(); // To save changes
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); boolean userFirstLogin = sharedPref.getBoolean("key_name", true); // Getting boolean value int pageNumber = sharedPref.getInt("key_name", 0); // Getting Integer value float amount = sharedPref.getFloat("key_name", null); // Getting Float value long distance = sharedPref.getLong("key_name", null); // Getting Long value String email = sharedPref.getString("key_name", null); // Getting String value
//Normal Way SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPref.edit(); editor.remove("key_name"); editor.commit(); //Optimized Way SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); sharedPref.edit().remove("key_name").commit();
//Normal Way SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPref.edit(); editor.clear(); editor.commit(); //Optimized Way SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); sharedPref.edit().clear().commit();
So, we are done with the explanation. Now we will see an example to implement SharedPreferences.
package com.sanktips.sharedpreferencesexample; import android.content.Context; import android.content.SharedPreferences; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Toast; public class MainActivity extends AppCompatActivity { SharedPreferences sharedPref; SharedPreferences.Editor editor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Created Single SharedPreferences sharedPref = MainActivity.this.getPreferences(Context.MODE_PRIVATE); } public void writeSharedPref(){ String introValue = "Welcome to Sanktips.com"; String aboutValue = "Android Development Tutorials"; editor = sharedPref.edit(); editor.putString("Intro_Key", introValue); editor.putString("About_Key", aboutValue); editor.commit(); Toast.makeText(MainActivity.this, "SharedPref Write Done", Toast.LENGTH_SHORT).show(); } public void readSharedPref(){ String readValue = sharedPref.getString("Intro_Key", null); Toast.makeText(MainActivity.this, readValue, Toast.LENGTH_SHORT).show(); } public void deleteSharedPrefBYKey(){ editor = sharedPref.edit(); editor.remove("Intro_Key"); editor.commit(); Toast.makeText(MainActivity.this, "Key Deleted", Toast.LENGTH_SHORT).show(); } public void deleteAllSharedPref(){ editor = sharedPref.edit(); editor.clear(); editor.commit(); Toast.makeText(MainActivity.this, "Deleted All SharedPreferences", Toast.LENGTH_SHORT).show(); } }
Hope you find this article helpful. Join us on Facebook, Twitter and Google+ to get more updates on Android Development Tutorials.