logo
logo
Sign in

User Session Management In Android

avatar
Expert App Devs
User Session Management In Android

Overview:-

The session helps you when you need to store client information outside your application, with the goal that when the following client utilizes your application, you can undoubtedly get back his subtleties and perform in a like manner. 


This should be possible in numerous ways. Be that as it may, the simplest and most pleasant method of doing this is through Shared Preferences. 


Shared Preferences:-

Shared Preferences permit you to save and recover information as key, value pairs. To utilize shared preference, you need to call a strategy getSharedPreferences() that profits a SharedPreference case highlighting the record that contains the upsides of preferences. 


Sharedpreferences = getSharedPreferences(CONFIG_PREF, Context.MODE_PRIVATE); 


You can save something in the shared preferences by utilizing the SharedPreferences.Editor class. You will call the alter technique for the SharedPreference example and will get it in a proofreader object. Its grammar is − 


Editor editor = sharedpreferences.edit(); 

editor.putString("key", "esteem"); 

editor.commit(); 


Aside from the putString technique, there are strategies accessible in the proofreader class that permits control of information inside shared preferences. They are recorded as follows − 


 Sr.No Mode & Description


  1. apply() 
  2. It is a theoretical strategy. It will submit your progressions back from proofreader to the sharedPreference object you are calling 
  3. clear() 
  4. It will eliminate all qualities from the editorial manager 
  5. remove(String key) 
  6. It will eliminate the worth whose key has been passed as a boundary 
  7. putLong(String key, long worth)
  8. It will save a long worth in an inclination editorial manager 
  9. putInt(String key, int esteem) 
  10. It will save a number worth in an inclination proofreader 
  11. putFloat(String key, float esteem)
  12. It will save a float esteem in an inclination proofreader 


Session Management through Shared Preferences


To perform meeting the board from shared preferences, we really want to check the qualities or information put away in shared preferences in the onResume strategy. In case we don't have the information, we will begin the application from the start as it is recently introduced. In any case, assuming we got the information, we will begin from where the client left it. It is shown in the model beneath − 


Putting away Data


You can save information into shared preferences utilizing the manager. Every one of the crude information types like booleans, floats, ints, aches, and strings are upheld. Call editor.commit() to save changes to shared preferences. 


editor.putBoolean("key_name", valid);      /Storing boolean - valid/bogus 

editor.putString("key_name", "string esteem");   /Storing string 

editor.putInt("key_name", "int esteem");      /Storing number 

editor.putFloat("key_name", "float esteem");      /Storing float 

editor.putLong("key_name", "long worth");      /Storing long 

editor.commit();               /submit changes 


Recovering Data


Information can be retrieved from saved preferences by calling the getString() (For string) technique. Recall this technique ought to be approached by Shared Preferences, not on Editor. 


# returns put away inclination esteem 

# If esteem is absent return second param esteem - For this situation invalid 


pref.getString("key_name", invalid);         /getting String 

pref.getInt("key_name", invalid);         /getting Integer 

pref.getFloat("key_name", invalid);         /getting Float 

pref.getLong("key_name", invalid);         /getting Long 

pref.getBoolean("key_name", invalid);      /getting boolean


Clearing/Deleting Data


Assuming you need to erase from shared preferences you can call remove("key_name") to erase that specific value. Assuming you need to erase every one of the information, call clear() 

editor.remove("name");      /will erase key name 

editor.remove("email");      /will erase key email 

editor.commit();         /submit changes 

Following will clean every one of the information off of shared preferences 

editor.clear(); 

collect
0
avatar
Expert App Devs
guide
Zupyak is the world’s largest content marketing community, with over 400 000 members and 3 million articles. Explore and get your content discovered.
Read more