{"id":37103,"date":"2024-11-01T09:54:53","date_gmt":"2024-11-01T09:54:53","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=37103"},"modified":"2024-11-01T11:36:39","modified_gmt":"2024-11-01T11:36:39","slug":"java-android-app-development-course-save-to-shared-preferences","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/37103\/","title":{"rendered":"Java Android App Development Course, Save to Shared Preferences"},"content":{"rendered":"<p><body><\/p>\n<p>\n        When developing on Android, there are often cases where you need to store certain settings or user information of the application.<br \/>\n        What is used at this time is <strong>SharedPreferences<\/strong>.<br \/>\n        SharedPreferences is useful for saving data using simple key-value pairs and is suitable for storing small amounts of data.<br \/>\n        In this tutorial, we will cover how to save and retrieve user information using SharedPreferences.\n    <\/p>\n<h2>1. What is SharedPreferences?<\/h2>\n<p>\n        SharedPreferences is a lightweight data storage mechanism in Android that makes it easy to save and recover application settings, user information, etc.<br \/>\n        It is suitable for storing small data sets, and the data is stored in the XML file format in the app&#8217;s data directory.\n    <\/p>\n<h2>2. Reasons to Use SharedPreferences<\/h2>\n<ul>\n<li>Simple data storage: It allows you to easily save simple information such as user settings and login information.<\/li>\n<li>Shareable: You can easily access shared values across multiple activities.<\/li>\n<li>Lightweight: It is useful when using a complex database is unnecessary.<\/li>\n<\/ul>\n<h2>3. Basic Usage of SharedPreferences<\/h2>\n<p>\n        The process of using SharedPreferences can be broadly divided into three steps:<br \/>\n        <strong>Creation<\/strong>, <strong>Data Saving<\/strong>, <strong>Data Retrieval<\/strong>.\n    <\/p>\n<h3>3.1 Creating SharedPreferences<\/h3>\n<p>\n        The method for creating SharedPreferences is as follows. The code below is an example of creating SharedPreferences in the `MainActivity` class.\n    <\/p>\n<pre><code>SharedPreferences sharedPreferences = getSharedPreferences(\"MyPreferences\", MODE_PRIVATE);<\/code><\/pre>\n<h3>3.2 Data Saving<\/h3>\n<p>\n        When saving data to SharedPreferences, you need to use the `Editor`. Below is an example of saving a username and age.\n    <\/p>\n<pre><code>\nSharedPreferences.Editor editor = sharedPreferences.edit();\neditor.putString(\"username\", \"John Doe\");\neditor.putInt(\"age\", 25);\neditor.apply();\n<\/code><\/pre>\n<h3>3.3 Data Retrieval<\/h3>\n<p>\n        To retrieve the saved data, use the `getString` or `getInt` method. The code below is an example of retrieving saved user information.\n    <\/p>\n<pre><code>\nString username = sharedPreferences.getString(\"username\", \"default\");\nint age = sharedPreferences.getInt(\"age\", 0);\n<\/code><\/pre>\n<h2>4. Practical Example<\/h2>\n<p>\n        Below is the example code utilizing SharedPreferences overall. We will create an app that saves information entered by the user and displays the saved information on the screen.\n    <\/p>\n<h3>4.1 Layout File (activity_main.xml)<\/h3>\n<pre><code>&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\n    android:orientation=\"vertical\"&gt;\n\n    &lt;EditText\n        android:id=\"@+id\/editTextUsername\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"\n        android:hint=\"Username\"\/&gt;\n\n    &lt;EditText\n        android:id=\"@+id\/editTextAge\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"\n        android:hint=\"Age\"\n        android:inputType=\"number\"\/&gt;\n\n    &lt;Button\n        android:id=\"@+id\/buttonSave\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"Save\"\/&gt;\n\n    &lt;TextView\n        android:id=\"@+id\/textViewResult\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\/&gt;\n\n    &lt;\/LinearLayout&gt;<\/code><\/pre>\n<h3>4.2 Main Activity (MainActivity.java)<\/h3>\n<pre><code>import android.content.SharedPreferences;\n    import android.os.Bundle;\n    import android.view.View;\n    import android.widget.Button;\n    import android.widget.EditText;\n    import android.widget.TextView;\n    import androidx.appcompat.app.AppCompatActivity;\n\n    public class MainActivity extends AppCompatActivity {\n\n        private SharedPreferences sharedPreferences;\n        private EditText editTextUsername, editTextAge;\n        private TextView textViewResult;\n\n        @Override\n        protected void onCreate(Bundle savedInstanceState) {\n            super.onCreate(savedInstanceState);\n            setContentView(R.layout.activity_main);\n\n            editTextUsername = findViewById(R.id.editTextUsername);\n            editTextAge = findViewById(R.id.editTextAge);\n            textViewResult = findViewById(R.id.textViewResult);\n            Button buttonSave = findViewById(R.id.buttonSave);\n\n            sharedPreferences = getSharedPreferences(\"MyPreferences\", MODE_PRIVATE);\n            loadStoredData();\n\n            buttonSave.setOnClickListener(new View.OnClickListener() {\n                @Override\n                public void onClick(View v) {\n                    String username = editTextUsername.getText().toString();\n                    int age = Integer.parseInt(editTextAge.getText().toString());\n                    saveData(username, age);\n                }\n            });\n        }\n\n        private void saveData(String username, int age) {\n            SharedPreferences.Editor editor = sharedPreferences.edit();\n            editor.putString(\"username\", username);\n            editor.putInt(\"age\", age);\n            editor.apply();\n            loadStoredData();\n        }\n\n        private void loadStoredData() {\n            String username = sharedPreferences.getString(\"username\", \"default\");\n            int age = sharedPreferences.getInt(\"age\", 0);\n            textViewResult.setText(\"Username: \" + username + \"\\nAge: \" + age);\n        }\n    }<\/code><\/pre>\n<h2>5. Precautions<\/h2>\n<p>\n        SharedPreferences is suitable for storing small amounts of data.<br \/>\n        If you need to store large amounts of data or complex data structures, it is recommended to use an SQLite database or the Room library.\n    <\/p>\n<h2>6. Conclusion<\/h2>\n<p>\n        In this tutorial, we learned how to save and retrieve simple user information using SharedPreferences.<br \/>\n        By utilizing these features, you can develop various applications that enhance the user experience.<br \/>\n        In the future, we will also cover how to use the Room library to efficiently manage more data.\n    <\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When developing on Android, there are often cases where you need to store certain settings or user information of the application. What is used at this time is SharedPreferences. SharedPreferences is useful for saving data using simple key-value pairs and is suitable for storing small amounts of data. In this tutorial, we will cover how &hellip; <a href=\"https:\/\/atmokpo.com\/w\/37103\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Java Android App Development Course, Save to Shared Preferences&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[137],"tags":[],"class_list":["post-37103","post","type-post","status-publish","format-standard","hentry","category-java-android-app-development"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Java Android App Development Course, Save to Shared Preferences - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/37103\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Android App Development Course, Save to Shared Preferences - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"When developing on Android, there are often cases where you need to store certain settings or user information of the application. What is used at this time is SharedPreferences. SharedPreferences is useful for saving data using simple key-value pairs and is suitable for storing small amounts of data. In this tutorial, we will cover how &hellip; \ub354 \ubcf4\uae30 &quot;Java Android App Development Course, Save to Shared Preferences&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/37103\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:54:53+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:36:39+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/37103\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/37103\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Java Android App Development Course, Save to Shared Preferences\",\"datePublished\":\"2024-11-01T09:54:53+00:00\",\"dateModified\":\"2024-11-01T11:36:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/37103\/\"},\"wordCount\":377,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Java Android app development\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/37103\/\",\"url\":\"https:\/\/atmokpo.com\/w\/37103\/\",\"name\":\"Java Android App Development Course, Save to Shared Preferences - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:54:53+00:00\",\"dateModified\":\"2024-11-01T11:36:39+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/37103\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/37103\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/37103\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Android App Development Course, Save to Shared Preferences\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java Android App Development Course, Save to Shared Preferences - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","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:\/\/atmokpo.com\/w\/37103\/","og_locale":"ko_KR","og_type":"article","og_title":"Java Android App Development Course, Save to Shared Preferences - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"When developing on Android, there are often cases where you need to store certain settings or user information of the application. What is used at this time is SharedPreferences. SharedPreferences is useful for saving data using simple key-value pairs and is suitable for storing small amounts of data. In this tutorial, we will cover how &hellip; \ub354 \ubcf4\uae30 \"Java Android App Development Course, Save to Shared Preferences\"","og_url":"https:\/\/atmokpo.com\/w\/37103\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:54:53+00:00","article_modified_time":"2024-11-01T11:36:39+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/37103\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/37103\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Java Android App Development Course, Save to Shared Preferences","datePublished":"2024-11-01T09:54:53+00:00","dateModified":"2024-11-01T11:36:39+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/37103\/"},"wordCount":377,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Java Android app development"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/37103\/","url":"https:\/\/atmokpo.com\/w\/37103\/","name":"Java Android App Development Course, Save to Shared Preferences - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:54:53+00:00","dateModified":"2024-11-01T11:36:39+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/37103\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/37103\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/37103\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Java Android App Development Course, Save to Shared Preferences"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/37103","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=37103"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/37103\/revisions"}],"predecessor-version":[{"id":37104,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/37103\/revisions\/37104"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=37103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=37103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=37103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}