{"id":22900,"date":"2018-11-26T06:36:10","date_gmt":"2018-11-26T06:36:10","guid":{"rendered":"https:\/\/www.knowband.com\/blog\/?p=22900\/"},"modified":"2018-11-26T22:39:44","modified_gmt":"2018-11-26T12:09:44","slug":"how-session-management-done-in-android","status":"publish","type":"post","link":"https:\/\/www.knowband.com\/blog\/tutorials\/how-session-management-done-in-android\/","title":{"rendered":"How is Session Management done in Android?"},"content":{"rendered":"<p style=\"text-align: justify;\">In Android, the session management is done using some data storage techniques. This data can be stored in some global variables, database or application&#8217;s preferences.<\/p>\n<p style=\"text-align: justify;\">But there is an issue with the global variables. The global variables can only retain the data until the application is opened. Once the application gets closed, the global variables get destroyed and their values are lost.<\/p>\n<p style=\"text-align: justify;\">The data stored in database and application preferences don&#8217;t get lost when the application is closed.<\/p>\n<p style=\"text-align: justify;\">So, these are used for session management. But data storage and access in the database is slower as compared to the preferences.<\/p>\n<p style=\"text-align: justify;\">For this reason, the preferences are used for session management.<\/p>\n<p style=\"text-align: justify;\">In the Android context, these preferences are known as SharedPreferences. These allow for faster data storage, access and updation.<\/p>\n<p style=\"text-align: justify;\">SharedPreferences are normally used to store the application&#8217;s settings.<\/p>\n<p style=\"text-align: justify;\">Find the steps below to use the SharedPreferences in the app to store and access data for maintaining the session.<\/p>\n<p>1. Initialize the SharedPreferences&#8217; Editor to store data in the application&#8217;s SharedPreferences.<\/p>\n<pre class=\"brush: actionscript3; gutter: true\">SharedPreferences prefs = getApplicationContext().getSharedPreferences(&quot;preferences-key-name&quot;, MODE_PRIVATE);\r\nSharedPreferences.Editor editor = prefs.edit();<\/pre>\n<p style=\"text-align: justify;\">2. Put the value in the editor object to save in the SharedPreferences. These values can be of the type &#8211; String, boolean, int, float, etc. After adding the values, execute the commit() method of Editor to save the values.<\/p>\n<pre class=\"brush: actionscript3; gutter: true\">editor.putString(&quot;key-username&quot;, &quot;John Doe&quot;);\r\neditor.commit();<\/pre>\n<p>3. To retrieve the data from the SharedPreferences, there is no need for the Editor object. Simply get the required String or any other value using the corresponding method.<\/p>\n<pre class=\"brush: actionscript3; gutter: true\">SharedPreferences prefs = getApplicationContext().getSharedPreferences(&quot;preferences-key-name &quot;, MODE_PRIVATE);\r\nString username = prefs.getString(&quot;key-username&quot;, &quot;default-value&quot;);<\/pre>\n<p style=\"text-align: justify;\">The second parameter of the getString() method is the default value which is returned, in case there is no value saved in the preferences corresponding to that key.<\/p>\n<p style=\"text-align: justify;\">4. To clear a particular SharedPreference key, use the remove() method.<\/p>\n<pre class=\"brush: actionscript3; gutter: true\">SharedPreferences prefs = getApplicationContext().getSharedPreferences(&quot;preferences-key-name&quot;, MODE_PRIVATE);\r\nSharedPreferences.Editor editor = prefs.edit();\r\neditor.remove(&quot;key-username&quot;);\r\neditor.commit();<\/pre>\n<p>5. To clear all the saved SharedPreferences, use the clear() method.<\/p>\n<pre class=\"brush: actionscript3; gutter: true\">SharedPreferences prefs = getApplicationContext().getSharedPreferences(&quot;preferences-key-name&quot;, MODE_PRIVATE);\r\nSharedPreferences.Editor editor = prefs.edit();\r\neditor.clear();\r\neditor.commit();<\/pre>\n<p style=\"text-align: justify;\">Find below the usage example of this SharedPreferences for session management:-<\/p>\n<p style=\"text-align: justify;\">When a user login into the application, save the username or any other corresponding value of the user in the SharedPreferences. Use this saved value to log in the user again when the application is reopened or this value can be accessed in any activity throughout the application to get the username. When the user clicks on Logout, clear the user-related fields to log out the user.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Android, the session management is done using some data storage techniques. This data can be stored in some global variables, database or application&#8217;s preferences. But there is an issue with the global variables. The global variables can only retain the data until the application is opened. Once the application gets closed, the global variables [&hellip;]<\/p>\n","protected":false},"author":16,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_kbui_enabled":"","footnotes":""},"categories":[7507],"tags":[8085,8084,8081],"class_list":["post-22900","post","type-post","status-publish","format-standard","hentry","category-tutorials","tag-session-management","tag-session-management-for-login-and-logout-in-android","tag-session-management-in-android"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How is Session Management done in Android? - Knowband Blog | Ecommerce Modules<\/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:\/\/www.knowband.com\/blog\/tutorials\/how-session-management-done-in-android\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How is Session Management done in Android? - Knowband Blog | Ecommerce Modules\" \/>\n<meta property=\"og:description\" content=\"In Android, the session management is done using some data storage techniques. This data can be stored in some global variables, database or application&#8217;s preferences. But there is an issue with the global variables. The global variables can only retain the data until the application is opened. Once the application gets closed, the global variables [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.knowband.com\/blog\/tutorials\/how-session-management-done-in-android\/\" \/>\n<meta property=\"og:site_name\" content=\"Knowband Blog | Ecommerce Modules\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Knowband\" \/>\n<meta property=\"article:published_time\" content=\"2018-11-26T06:36:10+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-11-26T12:09:44+00:00\" \/>\n<meta name=\"author\" content=\"Ajay Taneja\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@KnowbandPlugin\" \/>\n<meta name=\"twitter:site\" content=\"@KnowbandPlugin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ajay Taneja\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/tutorials\\\/how-session-management-done-in-android\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/tutorials\\\/how-session-management-done-in-android\\\/\"},\"author\":{\"name\":\"Ajay Taneja\",\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/#\\\/schema\\\/person\\\/091fe95729cba0da363b4fd2a784a893\"},\"headline\":\"How is Session Management done in Android?\",\"datePublished\":\"2018-11-26T06:36:10+00:00\",\"dateModified\":\"2018-11-26T12:09:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/tutorials\\\/how-session-management-done-in-android\\\/\"},\"wordCount\":374,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/#organization\"},\"keywords\":[\"session management\",\"session management for login and logout in android\",\"Session Management in Android\"],\"articleSection\":[\"Tutorials\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/tutorials\\\/how-session-management-done-in-android\\\/\",\"url\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/tutorials\\\/how-session-management-done-in-android\\\/\",\"name\":\"How is Session Management done in Android? - Knowband Blog | Ecommerce Modules\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/#website\"},\"datePublished\":\"2018-11-26T06:36:10+00:00\",\"dateModified\":\"2018-11-26T12:09:44+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/tutorials\\\/how-session-management-done-in-android\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.knowband.com\\\/blog\\\/tutorials\\\/how-session-management-done-in-android\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/tutorials\\\/how-session-management-done-in-android\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How is Session Management done in Android?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/#website\",\"url\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/\",\"name\":\"Knowband Blog | Ecommerce Modules\",\"description\":\"Magento, Prestashop, Opencart, Shopify\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/#organization\",\"name\":\"Knowband Blog | Ecommerce Modules\",\"url\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/wp-content\\\/uploads\\\/2016\\\/09\\\/cropped-knowband_logo-v2.png\",\"contentUrl\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/wp-content\\\/uploads\\\/2016\\\/09\\\/cropped-knowband_logo-v2.png\",\"width\":153,\"height\":61,\"caption\":\"Knowband Blog | Ecommerce Modules\"},\"image\":{\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/Knowband\",\"https:\\\/\\\/x.com\\\/KnowbandPlugin\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/fr\\\/#\\\/schema\\\/person\\\/091fe95729cba0da363b4fd2a784a893\",\"name\":\"Ajay Taneja\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/wp-content\\\/plugins\\\/wp-user-avatar\\\/deprecated\\\/wp-user-avatar\\\/images\\\/wpua-96x96.png\",\"url\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/wp-content\\\/plugins\\\/wp-user-avatar\\\/deprecated\\\/wp-user-avatar\\\/images\\\/wpua-96x96.png\",\"contentUrl\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/wp-content\\\/plugins\\\/wp-user-avatar\\\/deprecated\\\/wp-user-avatar\\\/images\\\/wpua-96x96.png\",\"caption\":\"Ajay Taneja\"},\"url\":\"https:\\\/\\\/www.knowband.com\\\/blog\\\/author\\\/ataneja\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How is Session Management done in Android? - Knowband Blog | Ecommerce Modules","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:\/\/www.knowband.com\/blog\/tutorials\/how-session-management-done-in-android\/","og_locale":"en_US","og_type":"article","og_title":"How is Session Management done in Android? - Knowband Blog | Ecommerce Modules","og_description":"In Android, the session management is done using some data storage techniques. This data can be stored in some global variables, database or application&#8217;s preferences. But there is an issue with the global variables. The global variables can only retain the data until the application is opened. Once the application gets closed, the global variables [&hellip;]","og_url":"https:\/\/www.knowband.com\/blog\/tutorials\/how-session-management-done-in-android\/","og_site_name":"Knowband Blog | Ecommerce Modules","article_publisher":"https:\/\/www.facebook.com\/Knowband","article_published_time":"2018-11-26T06:36:10+00:00","article_modified_time":"2018-11-26T12:09:44+00:00","author":"Ajay Taneja","twitter_card":"summary_large_image","twitter_creator":"@KnowbandPlugin","twitter_site":"@KnowbandPlugin","twitter_misc":{"Written by":"Ajay Taneja","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.knowband.com\/blog\/tutorials\/how-session-management-done-in-android\/#article","isPartOf":{"@id":"https:\/\/www.knowband.com\/blog\/tutorials\/how-session-management-done-in-android\/"},"author":{"name":"Ajay Taneja","@id":"https:\/\/www.knowband.com\/blog\/fr\/#\/schema\/person\/091fe95729cba0da363b4fd2a784a893"},"headline":"How is Session Management done in Android?","datePublished":"2018-11-26T06:36:10+00:00","dateModified":"2018-11-26T12:09:44+00:00","mainEntityOfPage":{"@id":"https:\/\/www.knowband.com\/blog\/tutorials\/how-session-management-done-in-android\/"},"wordCount":374,"commentCount":0,"publisher":{"@id":"https:\/\/www.knowband.com\/blog\/fr\/#organization"},"keywords":["session management","session management for login and logout in android","Session Management in Android"],"articleSection":["Tutorials"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.knowband.com\/blog\/tutorials\/how-session-management-done-in-android\/","url":"https:\/\/www.knowband.com\/blog\/tutorials\/how-session-management-done-in-android\/","name":"How is Session Management done in Android? - Knowband Blog | Ecommerce Modules","isPartOf":{"@id":"https:\/\/www.knowband.com\/blog\/fr\/#website"},"datePublished":"2018-11-26T06:36:10+00:00","dateModified":"2018-11-26T12:09:44+00:00","breadcrumb":{"@id":"https:\/\/www.knowband.com\/blog\/tutorials\/how-session-management-done-in-android\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.knowband.com\/blog\/tutorials\/how-session-management-done-in-android\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.knowband.com\/blog\/tutorials\/how-session-management-done-in-android\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.knowband.com\/blog\/fr\/"},{"@type":"ListItem","position":2,"name":"How is Session Management done in Android?"}]},{"@type":"WebSite","@id":"https:\/\/www.knowband.com\/blog\/fr\/#website","url":"https:\/\/www.knowband.com\/blog\/fr\/","name":"Knowband Blog | Ecommerce Modules","description":"Magento, Prestashop, Opencart, Shopify","publisher":{"@id":"https:\/\/www.knowband.com\/blog\/fr\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.knowband.com\/blog\/fr\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.knowband.com\/blog\/fr\/#organization","name":"Knowband Blog | Ecommerce Modules","url":"https:\/\/www.knowband.com\/blog\/fr\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.knowband.com\/blog\/fr\/#\/schema\/logo\/image\/","url":"https:\/\/www.knowband.com\/blog\/wp-content\/uploads\/2016\/09\/cropped-knowband_logo-v2.png","contentUrl":"https:\/\/www.knowband.com\/blog\/wp-content\/uploads\/2016\/09\/cropped-knowband_logo-v2.png","width":153,"height":61,"caption":"Knowband Blog | Ecommerce Modules"},"image":{"@id":"https:\/\/www.knowband.com\/blog\/fr\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/Knowband","https:\/\/x.com\/KnowbandPlugin"]},{"@type":"Person","@id":"https:\/\/www.knowband.com\/blog\/fr\/#\/schema\/person\/091fe95729cba0da363b4fd2a784a893","name":"Ajay Taneja","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.knowband.com\/blog\/wp-content\/plugins\/wp-user-avatar\/deprecated\/wp-user-avatar\/images\/wpua-96x96.png","url":"https:\/\/www.knowband.com\/blog\/wp-content\/plugins\/wp-user-avatar\/deprecated\/wp-user-avatar\/images\/wpua-96x96.png","contentUrl":"https:\/\/www.knowband.com\/blog\/wp-content\/plugins\/wp-user-avatar\/deprecated\/wp-user-avatar\/images\/wpua-96x96.png","caption":"Ajay Taneja"},"url":"https:\/\/www.knowband.com\/blog\/author\/ataneja\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.knowband.com\/blog\/wp-json\/wp\/v2\/posts\/22900","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.knowband.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.knowband.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.knowband.com\/blog\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/www.knowband.com\/blog\/wp-json\/wp\/v2\/comments?post=22900"}],"version-history":[{"count":5,"href":"https:\/\/www.knowband.com\/blog\/wp-json\/wp\/v2\/posts\/22900\/revisions"}],"predecessor-version":[{"id":22919,"href":"https:\/\/www.knowband.com\/blog\/wp-json\/wp\/v2\/posts\/22900\/revisions\/22919"}],"wp:attachment":[{"href":"https:\/\/www.knowband.com\/blog\/wp-json\/wp\/v2\/media?parent=22900"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.knowband.com\/blog\/wp-json\/wp\/v2\/categories?post=22900"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.knowband.com\/blog\/wp-json\/wp\/v2\/tags?post=22900"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}