Android: Manage User Session using Shared Preferences
In this post, I will show you how to maintain user session in an Android App.
In other words, the user login session will be maintained in an app even if the app is closed or crashed. The user will always be in a logged in state until he logouts. This functionality can be found in a lot of application like Facebook, Twitter etc.
We will implement this by using SharedPreferences, in shared preferences the data is stored and retrieved in key-value pair.
Overview:
We will create 2 screens-
- Login
2. Welcome
The user will enter credentials, if the credentials are valid then the user will get logged in. We will set ‘LoggedInStatus’ to ‘true’ in Shared Preferences.
When the logouts, will set the ‘LoggedInStatus’ to ‘false’.
Note: Please check inline comments for better understanding
Login Activity
Welcome Activity
Shared Preferences
I have used Retrofit Client for API call.
For complete reference please the check:
❤ Please hit clap if you learned something!
If you have any questions leave a comment below or message me on Linkedin.