

{"id":77783,"date":"2020-05-12T12:20:43","date_gmt":"2020-05-12T06:50:43","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=77783"},"modified":"2021-08-25T13:56:07","modified_gmt":"2021-08-25T08:26:07","slug":"shared-preference-in-android","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/shared-preference-in-android\/","title":{"rendered":"Shared Preference in Android &#8211; Example and Implementation"},"content":{"rendered":"<p>Welcome back to <strong>Android DataFlair series.<\/strong> In this article, we\u2019ll read about <strong>Preferences and Shared preference in Android<\/strong>. As android provides us with many ways to store data, One of the best ways of storing data provided by Android is Preferences. The reason is that it saves the data in the form of a Key-Value pair.<\/p>\n<p>So let&#8217;s start learning this interesting concept.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/Shared-Preferences-in-Android.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-77859 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/Shared-Preferences-in-Android.jpg\" alt=\"Shared Preference in Android\" width=\"802\" height=\"420\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/Shared-Preferences-in-Android.jpg 802w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/Shared-Preferences-in-Android-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/Shared-Preferences-in-Android-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/Shared-Preferences-in-Android-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/Shared-Preferences-in-Android-520x272.jpg 520w\" sizes=\"auto, (max-width: 802px) 100vw, 802px\" \/><\/a><\/p>\n<h2>Preferences in Android<\/h2>\n<p>First of all, we\u2019ll begin with Android Preferences and then we\u2019ll move towards Android Shared preferences. Preferences are basically used to keep track of the application along with the user preferences. A preference is a simple key-value pair with a specific data type for the value. The task of preference key is to uniquely identify a preference and on the other hand values store the preference values.<\/p>\n<p><strong>To understand it in a better way:<\/strong><\/p>\n<p>Suppose you want to store the Mobile Number of a user. For that you can consider choosing the data type of Integer. The key name can be \u201cMobileNumber\u201d. And the Value can be in Integers values, such as \u201c7894561230\u201d or \u201c0278-471220\u201d.<\/p>\n<p>For preferences, The data types that can be chosen are as follows:<\/p>\n<ul>\n<li>Boolean Types<\/li>\n<li>Integer Types<\/li>\n<li>Float Types<\/li>\n<li>String Types<\/li>\n<li>Long Types<\/li>\n<\/ul>\n<h2>APIs for accessing Preferences in Android<\/h2>\n<p>There are three APIs from which one can be chosen to get access to the preferences. These three APIs are:<\/p>\n<ul>\n<li><strong>getPreference():<\/strong> It is used within the Activity, to access activity-specific preference.<\/li>\n<li><strong>getSharedPreference():<\/strong> It is used from the Activity or some other application context. Its use is to access application-level preferences.<\/li>\n<li><strong>getDefaultSharedPreference():<\/strong> It is used on the PreferenceManager, to get the shared preferences that goes with the overall Android preference framework.<\/li>\n<\/ul>\n<h2>Shared Preference in Android<\/h2>\n<p>Before getting started with it, let us first understand its meaning in a better way. So it comes with two words, Shared and Preferences. Here, Share means distributing the data and Preferences here means important or something that is preferable.<\/p>\n<p>To use it, we need to call the method <strong>getSharedPreferences()<\/strong> that returns a SharedPreference instance pointing to the file containing the values of preferences. This is done as follows:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">SharedPreferences sharedpreference_name = getSharedPreferences(String name, MODE_PRIVATE);<\/pre>\n<p>In this method, we\u2019ve passed two parameters: the first parameter is the key name of the SharedPreference and the second parameter is the MODE. The mode by default is Private. Other than private mode, there are six modes in total that are as below:<\/p>\n<p><strong>1. MODE_APPEND<\/strong><\/p>\n<p>It appends new preferences in the pre-existing preferences.<\/p>\n<p><strong>2. MODE_ENABLE_WRITE_AHEAD _LOGGING<\/strong><\/p>\n<p>It enables write ahead logging by default, if it is set true. It is a database flag.<\/p>\n<p><strong>3. MODE_WORLD_READABLE<\/strong><\/p>\n<p>This mode sets read mode for other applications to read the preferences.<\/p>\n<p><strong>4. MODE_WORLD_WRITABLE<\/strong><\/p>\n<p>This mode sets write mode for other applications to write the preferences.<\/p>\n<p><strong>5. MODE_MULTI_PROCESS<\/strong><\/p>\n<p>It checks for modifications even if the preference instance is already loaded.<\/p>\n<p><strong>6. MODE_PRIVATE<\/strong><\/p>\n<p>It is the default mode. In this mode, we can access a file only by the calling application.<\/p>\n<h2>Android Shared Preferences methods<\/h2>\n<p>Let us now see the various methods in shared preferences in android:<\/p>\n<ul>\n<li><strong>contains( String key):<\/strong> It checks if the preference contains a preference or not.<\/li>\n<li><strong>edit():<\/strong> It creates a new Editor for preferences, to enable modifications to the data and commit changes back to the SharedPreferences atomically.<\/li>\n<li><strong>getAll():<\/strong> It retrieves all the values from the preferences.<\/li>\n<li><strong>getBoolean(String key, boolean defValue):<\/strong> It retrieves a boolean value from the preferences.<\/li>\n<li><strong>getFloat(String key, float defValue):<\/strong> It retrieves a float value from the preferences.<\/li>\n<li><strong>getInt(String key, int defValue):<\/strong> It retrieves an int value from the preferences.<\/li>\n<li><strong>getLong(String key, long defValue):<\/strong> It retrieves a long value from the preferences.<\/li>\n<li><strong>getString(String key, String defValue):<\/strong> It retrieves a String value from the preferences.<\/li>\n<li><strong>getStringSet(String key, Set defValue):<\/strong> It retrieves a Set of String values from the preferences.<\/li>\n<\/ul>\n<h2>Differences between Preferences and Shared Preferences in Android<\/h2>\n<p>Let us now see the comparison between Android Preference and Shared Preference:<\/p>\n<table>\n<tbody>\n<tr>\n<td><b>Preferences<\/b><\/td>\n<td><b>Shared Preferences<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Preference is a package that provides classes for preferences management.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Shared preferences stores data values in the XML file.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">This data is stored persistently in implementation- dependent backing store.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Android maintains these XML files for the users.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">The data in preferences doesn&#8217;t change when the device reboots.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">The data is not encrypted &amp; changes as the device restarts.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Preferences are good for Sensitive Data.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Shared preference is not preferable for Sensitive Data.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Preferences pref = getPreferenceScreen().getPreference();<\/span><\/td>\n<td><span style=\"font-weight: 400;\">SharedPreferences sp =getPreferenceScreen().getSharedPreference();<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Shared preference in Android Step by Step Implementation<\/h2>\n<p>Now we\u2019ll implement Shared Preferences using the following steps:<br \/>\n1. First of all, we\u2019ll create a new project and name it.<br \/>\n2. Now we\u2019ll write the following code in the activity_main.xml file.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n   android:layout_width=\"match_parent\"\r\n   android:layout_height=\"match_parent\"\r\n   android:orientation=\"vertical\"&gt;\r\n\r\n   &lt;TextView\r\n       android:id=\"@+id\/textView\"\r\n       android:layout_width=\"wrap_content\"\r\n       android:layout_height=\"wrap_content\"\r\n       android:layout_centerHorizontal=\"true\"\r\n       android:layout_marginLeft=\"90dp\"\r\n       android:layout_marginTop=\"30dp\"\r\n       android:text=\"DataFlair \"\r\n       android:textColor=\"#00574B\"\r\n       android:textSize=\"50dp\"\r\n       android:textStyle=\"bold\" \/&gt;\r\n\r\n   &lt;TextView\r\n       android:id=\"@+id\/text\"\r\n       android:layout_width=\"wrap_content\"\r\n       android:layout_height=\"wrap_content\"\r\n       android:layout_marginLeft=\"100dp\"\r\n       android:layout_marginTop=\"80dp\"\r\n       android:text=\"Enter your UserName\" \/&gt;\r\n\r\n   &lt;EditText\r\n       android:id=\"@+id\/Name\"\r\n       android:layout_width=\"wrap_content\"\r\n       android:layout_height=\"wrap_content\"\r\n       android:layout_marginLeft=\"100dp\"\r\n       android:ems=\"10\" \/&gt;\r\n\r\n   &lt;TextView\r\n       android:id=\"@+id\/text2\"\r\n       android:layout_width=\"wrap_content\"\r\n       android:layout_height=\"wrap_content\"\r\n       android:layout_marginLeft=\"100dp\"\r\n       android:text=\"Enter your Password\" \/&gt;\r\n\r\n   &lt;EditText\r\n       android:id=\"@+id\/password\"\r\n       android:layout_width=\"wrap_content\"\r\n       android:layout_height=\"wrap_content\"\r\n       android:layout_marginLeft=\"100dp\"\r\n       android:ems=\"10\"\r\n       android:inputType=\"textPassword\" \/&gt;\r\n\r\n   &lt;Button\r\n       android:id=\"@+id\/Login\"\r\n       android:layout_width=\"wrap_content\"\r\n       android:layout_height=\"wrap_content\"\r\n       android:layout_marginLeft=\"100dp\"\r\n       android:text=\"Login\" \/&gt;\r\n&lt;\/LinearLayout&gt;\r\n<\/pre>\n<p>3.\u00a0Now, write the following code in the MainActivity.java file.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.dataflair.mysharedpreference;\r\n\r\nimport android.content.Intent;\r\nimport android.content.SharedPreferences;\r\nimport android.os.Bundle;\r\nimport android.view.View;\r\nimport android.widget.Button;\r\nimport android.widget.EditText;\r\nimport android.widget.Toast;\r\n\r\nimport androidx.appcompat.app.AppCompatActivity;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n   EditText username, pass;\r\n   Button Login_Button;\r\n   SharedPreferences Shared_pref;\r\n   Intent intent;\r\n\r\n   @Override\r\n   protected void onCreate(Bundle savedInstanceState) {\r\n       super.onCreate(savedInstanceState);\r\n       setContentView(R.layout.activity_main);\r\n       username = findViewById(R.id.Name);\r\n       pass = findViewById(R.id.password);\r\n       Login_Button = findViewById(R.id.Login);\r\n       Shared_pref = getSharedPreferences(\"details\", MODE_PRIVATE);\r\n       intent = new Intent(MainActivity.this, secondActivity.class);\r\n       if (Shared_pref.contains(\"username\") &amp;&amp; Shared_pref.contains(\"password\")) {\r\n           startActivity(intent);\r\n       }\r\n       Login_Button.setOnClickListener(new View.OnClickListener() {\r\n           @Override\r\n           public void onClick(View v) {\r\n               String username = MainActivity.this.username.getText().toString();\r\n               String password = pass.getText().toString();\r\n               if (username.equals(\"Akshita Shrivastava\") &amp;&amp; password.equals(\"00000000\")) {\r\n                   SharedPreferences.Editor editor = Shared_pref.edit();\r\n                   editor.putString(\"username\", username);\r\n                   editor.putString(\"password\", password);\r\n                   editor.commit();\r\n                   Toast.makeText(getApplicationContext(), \"Logged in\", Toast.LENGTH_SHORT).show();\r\n                   startActivity(intent);\r\n               } else {\r\n                   Toast.makeText(getApplicationContext(), \"Enter Right Credentials\", Toast.LENGTH_SHORT).show();\r\n               }\r\n           }\r\n       });\r\n   }\r\n}\r\n<\/pre>\n<p>4.\u00a0Now create a new layout file second.xml in the resource\/ layout\/. And write the following code there:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n   android:layout_width=\"match_parent\"\r\n   android:layout_height=\"match_parent\"\r\n   android:orientation=\"vertical\"&gt;\r\n\r\n   &lt;TextView\r\n       android:id=\"@+id\/res_text\"\r\n       android:layout_width=\"wrap_content\"\r\n       android:layout_height=\"wrap_content\"\r\n       android:layout_gravity=\"center\"\r\n       android:layout_marginTop=\"170dp\"\r\n       android:textSize=\"22dp\" \/&gt;\r\n\r\n   &lt;Button\r\n       android:id=\"@+id\/LogOut\"\r\n       android:layout_width=\"wrap_content\"\r\n       android:layout_height=\"wrap_content\"\r\n       android:layout_gravity=\"center\"\r\n       android:layout_marginTop=\"25dp\"\r\n       android:text=\"Log Out\" \/&gt;\r\n&lt;\/LinearLayout&gt;<\/pre>\n<p>5.\u00a0Create a new java file secondActivity.java in the same folder as that of MainActivity.java. Write the following code in the secondActivity.java file:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.dataflair.mysharedpreference;\r\n\r\nimport android.content.Intent;\r\nimport android.content.SharedPreferences;\r\nimport android.os.Bundle;\r\nimport android.view.View;\r\nimport android.widget.Button;\r\nimport android.widget.TextView;\r\n\r\nimport androidx.appcompat.app.AppCompatActivity;\r\n\r\npublic class secondActivity extends AppCompatActivity {\r\n   SharedPreferences newPreference;\r\n   Intent newIntent;\r\n\r\n   @Override\r\n   protected void onCreate(Bundle savedInstanceState) {\r\n       super.onCreate(savedInstanceState);\r\n       setContentView(R.layout.second);\r\n       TextView result = findViewById(R.id.res_text);\r\n       Button LogOut_btn = findViewById(R.id.LogOut);\r\n       newPreference = getSharedPreferences(\"user_details\", MODE_PRIVATE);\r\n       newIntent = new Intent(secondActivity.this, MainActivity.class);\r\n       result.setText(\"Welcome, \" + newPreference.getString(\"username\", null));\r\n       LogOut_btn.setOnClickListener(new View.OnClickListener() {\r\n           @Override\r\n           public void onClick(View v) {\r\n               SharedPreferences.Editor edit = newPreference.edit();\r\n               edit.clear();\r\n               edit.commit();\r\n               startActivity(newIntent);\r\n           }\r\n       });\r\n   }\r\n}\r\n<\/pre>\n<p>6.\u00a0Write the following in the Manifest.xml File:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;manifest xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n   package=\"com.dataflair.mysharedpreference\"&gt;\r\n\r\n   &lt;application\r\n       android:allowBackup=\"true\"\r\n       android:icon=\"@mipmap\/ic_launcher\"\r\n       android:label=\"@string\/app_name\"\r\n       android:roundIcon=\"@mipmap\/ic_launcher_round\"\r\n       android:theme=\"@style\/AppTheme\"&gt;\r\n       &lt;activity android:name=\".MainActivity\"&gt;\r\n           &lt;intent-filter&gt;\r\n               &lt;action android:name=\"android.intent.action.MAIN\" \/&gt;\r\n               &lt;category android:name=\"android.intent.category.LAUNCHER\" \/&gt;\r\n           &lt;\/intent-filter&gt;\r\n       &lt;\/activity&gt;\r\n       &lt;activity\r\n           android:name=\".secondActivity\"\r\n           android:label=\"Shared Preferences - Details\"&gt;&lt;\/activity&gt;\r\n   &lt;\/application&gt;\r\n&lt;\/manifest&gt;\r\n<\/pre>\n<p>7.\u00a0Write the following code in colors file present here res\/values\/colors.xml:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;resources&gt;\r\n   &lt;color name=\"colorPrimary\"&gt;#00796B&lt;\/color&gt;\r\n   &lt;color name=\"colorPrimaryDark\"&gt;#01554B&lt;\/color&gt;\r\n   &lt;color name=\"colorAccent\"&gt;#C2185B&lt;\/color&gt;\r\n&lt;\/resources&gt;\r\n<\/pre>\n<p>8.\u00a0Now, run the code:<br \/>\na. This is our application<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/A1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-77860\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/A1.png\" alt=\"Preferences in Android\" width=\"300\" height=\"533\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/A1.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/A1-84x150.png 84w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/A1-169x300.png 169w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>b.\u00a0First, we entered the wrong credentials and following came up:<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/A2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-77861\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/A2.png\" alt=\"Shared Preferences in Android\" width=\"300\" height=\"533\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/A2.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/A2-84x150.png 84w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/A2-169x300.png 169w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>c.\u00a0Then, enter the right credentials and press Login-<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/A3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-77862\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/A3.png\" alt=\"Android Shared Preferences\" width=\"300\" height=\"533\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/A3.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/A3-84x150.png 84w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/A3-169x300.png 169w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>d.\u00a0Successful login is done:<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/A4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-77863\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/A4.png\" alt=\"Android Preferences\" width=\"300\" height=\"533\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/A4.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/A4-84x150.png 84w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/A4-169x300.png 169w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<h2>Summary<\/h2>\n<p>Finally, in this article, we have read about Android Preferences and Shared Preferences. While reading we covered many things about Shared Preference in Android. We saw how we can declare it and what are parameters we can pass in it. We then read its methods and the modes it works in. Then we also saw the differences between preferences and shared preferences. In the end, we implemented it and now it is your turn to implement preference in Android.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome back to Android DataFlair series. In this article, we\u2019ll read about Preferences and Shared preference in Android. As android provides us with many ways to store data, One of the best ways of&#46;&#46;&#46;<\/p>\n","protected":false},"author":10,"featured_media":77859,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18722],"tags":[22262,22264,22265,22266,22263],"class_list":["post-77783","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android","tag-android-preferences","tag-android-shared-preferences","tag-android-sharedpreferences-example","tag-shared-preference","tag-shared-preference-in-android"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Shared Preference in Android - Example and Implementation - DataFlair<\/title>\n<meta name=\"description\" content=\"Android preferences -Learn what is preference in android, shared preference in android, comparison between them, implementation of Android shared preference\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/shared-preference-in-android\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Shared Preference in Android - Example and Implementation - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Android preferences -Learn what is preference in android, shared preference in android, comparison between them, implementation of Android shared preference\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/shared-preference-in-android\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-05-12T06:50:43+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-08-25T08:26:07+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/Shared-Preferences-in-Android.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Shared Preference in Android - Example and Implementation - DataFlair","description":"Android preferences -Learn what is preference in android, shared preference in android, comparison between them, implementation of Android shared preference","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/shared-preference-in-android\/","og_locale":"en_US","og_type":"article","og_title":"Shared Preference in Android - Example and Implementation - DataFlair","og_description":"Android preferences -Learn what is preference in android, shared preference in android, comparison between them, implementation of Android shared preference","og_url":"https:\/\/data-flair.training\/blogs\/shared-preference-in-android\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2020-05-12T06:50:43+00:00","article_modified_time":"2021-08-25T08:26:07+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/Shared-Preferences-in-Android.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/shared-preference-in-android\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/shared-preference-in-android\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/a90b082e16aa38d207212d22b0581f33"},"headline":"Shared Preference in Android &#8211; Example and Implementation","datePublished":"2020-05-12T06:50:43+00:00","dateModified":"2021-08-25T08:26:07+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/shared-preference-in-android\/"},"wordCount":980,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/shared-preference-in-android\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/Shared-Preferences-in-Android.jpg","keywords":["android preferences","android shared preferences","android sharedpreferences example","shared preference","shared preference in android"],"articleSection":["Android Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/shared-preference-in-android\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/shared-preference-in-android\/","url":"https:\/\/data-flair.training\/blogs\/shared-preference-in-android\/","name":"Shared Preference in Android - Example and Implementation - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/shared-preference-in-android\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/shared-preference-in-android\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/Shared-Preferences-in-Android.jpg","datePublished":"2020-05-12T06:50:43+00:00","dateModified":"2021-08-25T08:26:07+00:00","description":"Android preferences -Learn what is preference in android, shared preference in android, comparison between them, implementation of Android shared preference","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/shared-preference-in-android\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/shared-preference-in-android\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/shared-preference-in-android\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/Shared-Preferences-in-Android.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/05\/Shared-Preferences-in-Android.jpg","width":802,"height":420,"caption":"Shared Preferences in Android"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/shared-preference-in-android\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Android Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/android\/"},{"@type":"ListItem","position":3,"name":"Shared Preference in Android &#8211; Example and Implementation"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/a90b082e16aa38d207212d22b0581f33","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/dd6de0d647a0185cd6faf264e4ba860b0d85d08d7070766f9cd41bea5bb0b227?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/dd6de0d647a0185cd6faf264e4ba860b0d85d08d7070766f9cd41bea5bb0b227?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/dd6de0d647a0185cd6faf264e4ba860b0d85d08d7070766f9cd41bea5bb0b227?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"The DataFlair Team is passionate about delivering top-notch tutorials and resources on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. With expertise in the tech industry, we simplify complex topics to help learners excel. Stay updated with our latest insights.","url":"https:\/\/data-flair.training\/blogs\/author\/dfadteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/77783","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=77783"}],"version-history":[{"count":4,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/77783\/revisions"}],"predecessor-version":[{"id":86550,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/77783\/revisions\/86550"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/77859"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=77783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=77783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=77783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}