Agenda
Today we will discuss how to store user's data permanently in your Android app.
Examples of Shared Preferences
You might have seen various applications where you login and even if you close the application and when you open it again you are still loged in so that is done by saving the data permanently. If this feature is absent in any aplication then user might need to login again and again to use that app so their are various ways to store data permanently which we will discuss below
Ways to store data in your application
Shared preferences
SQL Lite Databases and other cache memory
but we will focus today on Shared preferences
Shared Preference
Shared preferences is basically a file in your phone and it keeps the data in the form of a 'key' and 'value' just like JSON or we use "Map"or "Dictionary" in Python.
Remember that it is applcation specific, which means the data can be lost by performing following otions:
- on uninstalling the app
on clearing the application data through Android Settings
How to Access Shared Preferences
their are three methods in to access shared preferences
getPreferences(): used within 1 acitvity(in basic word activity is a screen that appears when user click any button in his app however default activity is MainActivity() which is usually homescreen of an app ) only
getSharedPreferences(): This is used for whole application level, this is mainly used by app developers
getDefaultSharedPreferences(): This works with over all android
Basic Syntax of getSharedPreferences()
getSharedPreferences(String PREFS_NAME,int mode)
here,
PREFS_NAME is the name of the file which will be saved in your android code and
mode is the operating mode, their are many modes but we will deal with private mode only.
we will discuss about sharedpreferences later on and try to build an app which will store user data simillar to like this.
Conclusion
Today we learned about storing user data permanently in an Android app. We discussed different methods of storing data, with a focus on Shared Preferences. Shared Preferences is a file-based storage system that stores data in key-value pairs.
Examples of Shared Preferences include applications that remember user login information, allowing users to stay logged in even when they close and reopen the app. Shared Preferences is application-specific, meaning the data can be lost if the app is uninstalled or if the application data is cleared through Android Settings.
To access Shared Preferences, there are three methods: getPreferences() for use within a single activity, getSharedPreferences() for application-wide usage, and getDefaultSharedPreferences() for overall Android usage.
The basic syntax for getSharedPreferences() is getSharedPreferences(String PREFS_NAME, int mode), where PREFS_NAME is the name of the file that will be saved in the Android code, and mode refers to the operating mode. We focused on private mode, but there are other modes available.
In our next steps, we will go deeper into Shared Preferences and build an app that demonstrates how to store user data using this method.