What are SharedPreferences?
SharedPreferences are key-value group of general data types that are saved in a folder
within an apps file structure. You can access this file from any place within the app to
either put data into the file or take data out of the file. You can’t access the file from
another app so it is secure from that point of view.
within an apps file structure. You can access this file from any place within the app to
either put data into the file or take data out of the file. You can’t access the file from
another app so it is secure from that point of view.
Where would you use SharedPreferences?
You would for example use SharedPreferences in a game where you would save the
user’s name, high score, and state of the game when they logged off. Then the next time
they log in, their score and game level would be recover from the desire file and they will
continue the game from where they ended it when they logged off.
user’s name, high score, and state of the game when they logged off. Then the next time
they log in, their score and game level would be recover from the desire file and they will
continue the game from where they ended it when they logged off.
Save your data in a single or multiple SharedPreferences files
You can save your desire data in a single file or multiple files, based on your needs. The
process is the same object for the SharedPreferences object that you get.In the case of a
single file, call getPreferences() to get a SharedPreferences object and for multiple files,
call getSharedPreferences() and pass it a name for the file as a parameter.
process is the same object for the SharedPreferences object that you get.In the case of a
single file, call getPreferences() to get a SharedPreferences object and for multiple files,
call getSharedPreferences() and pass it a name for the file as a parameter.
- getPreferences() - for Activity level preferences. Each action will have its own
- getSharedPreferences() - for application-level preferences. You can access to
Retrieving the data
Once you have the SharedPreferences object, you can use a number of get<type> method
for restore the values.
for restore the values.
Uses of Preference share
A Shared Preference file is a simple XML file which prefer your application. You can store
key-value pairs of data which your app requires often.
key-value pairs of data which your app requires often.
Most often, it can be used as some sort of cache/cookie which will be useful when the
user's detail/action is to be retained when he comes back to use the application.
user's detail/action is to be retained when he comes back to use the application.
Some of the common uses of shared preferences :
1. If any user wants to use shared preferences to check login, and if the user is logged in
to your application you can direct that to the dashboard and if not, show them the login
page.
to your application you can direct that to the dashboard and if not, show them the login
page.
2. Show the first time users the demo of your app. Most of the apps today shows the users
who opens the app for the first time about the app, how to use it and what is where kind
of things.
who opens the app for the first time about the app, how to use it and what is where kind
of things.
3. Some information which is required across different activities and it is hard to pass
through design and not important enough to store in DB.
through design and not important enough to store in DB.
Shared preference are mostly for temporary storage in Android . We are
providing Android Training in Chandigarh at CBitss Technologies, Practical training
education assured.
providing Android Training in Chandigarh at CBitss Technologies, Practical training
education assured.
0 comments:
Post a Comment