This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to use getSharedPreferences in android
I am new Android Developer. I have a query regarding Shared Preference.
How long data will be persisted in 'SharedPreference' of Android App?
My requirement is to persist data for a a particular user until he log-off from the app. If same user or new user logs-in from same or new device, he/ she should have no content stored in Shared Preference from earlier session.
Please guide me to develop this. Thanks
SharedPreferences will persist data until you clear/overwrite the data by yourself or until the user uninstalls the app or you clear app's cache.
With respect to your question, when the user logs off from the app you can perform SharedPreferences.Editor.clear() followed by a commit() to remove all saved data of the current user from shared preference.
More info. here: SharedPreferences.Editor clear ()
The Data which you saved in SharedPreference will store until you removed it by programmatically (changing the preference value to null).. or until you clear the history of the app or until uninstall the app ...
Related
This question already has answers here:
Determine if Android app is being used for the first time
(15 answers)
Closed 2 years ago.
I want user to input some information on the first time they start the app so that those information can be used from that on. What are the possible ways to do this?
if you are looking at storing large or complicated data you can save it with room database. if you need to store simple data you can use shared preferences.
you can save a variable in shared preferences such as isFirstTimeUser=true or false then you can check this variable when the app starts. If it is the users first time to use the app present them with ui to create the fields you require. If not then proceed to fetch the details
This question already has answers here:
How to disable the Clear Data button in Application info of Manage application
(4 answers)
Closed 4 years ago.
I am creating an android application which i have store some data in to store preferences,Actually i want to prevent that for further work.
Now what happen user can easily delete that data from app setting.
Is there any way to disable or hide that button from app setting?
You can create a SQLite database file in the external storage so it will not be deleted when the user clears cache or even delete App Data. But you can not prevent any of these to be deleted. User has full control of what can be on the device and what not.
Edit
As #NileshRathod mentioned in the comments below, you can redirect a user to an Activity to manage what could be deleted and what not simply by adding the following in your Android Manifest:
<!--Prevent user from deleting App Preferences-->
<application ... android:manageSpaceActivity=".MyManageSpaceActivity">
I'm developing an Application using Firebase analytics, authentication and DB services.
I need to save some preferences and some settings, related to the user.
Most of them are managed in a "SettingsActivity" which is similar to the sample one provided by Android Studio, but with this configuration if the user changes the device, those preferences are lost.
The app should provide a customizable experience for each user. To achieve this I'd like my preferences to be saved on Firebase insted of in local Preferences.
How can I achieve this without have to change too much my app structure?
EDIT:
Here's an example:
The user download my App on first device (Dev-A), he sign up, then he goes to settings and set first setting from A to B, and second setting from A to D.
Then he decide to download my App on a second device (Dev-B), he log in, then he goes to settings.
On Dev-A, first setting is set to B and second setting is set to D.
On Dev-B, first setting is set to A and second setting is set to A.
You will need to store this information in Firebase Database.
Use the user'd UID you get from Firebase Auth in the Database to store the settings.
The data should be under '/users/{uid}'
This way, you will persist all user specific data across devices. (Since the same UID will be presented to same authentication account)
Check this link in Firebase documentation to set the database security
You should try this library:
A implementation of SharedPreferences which syncs with Firebase database to keep your SharedPreferences in sync between multiple devices of one user and/or to back them up for app re-installs!
I am a web developer and new to android and studying ... For first step I made an user login system using PHP-MySQL web services.
The application communicating with the web service pretty well and check for user exists. Now if the user exists it will return the user details if user exists.
My question is like in web application is there any session / cookie handling in android application ? For eg if I can save the session then not need to login at each and every time.
I am sure there is a way to do this because lots of apps are working with this feature. But since I am new to android please advice a bit.
Not knowing a correct word to googling. Is that "SavePreferences".
Thanks in advance
I think what you are looking for is SharedPreferences. This stores data persistently in a (key, value) pair so you could say have a check box at the login screen so if they check it then it stores a boolean as the value and their username as the key. Then when they get to the login screen and choose their username it checks that value and if it is true then it doesn't require a password. This is assuming that you allow more than one user to login from the app.
However, if you only have one person logging in from the app to that device, then you could send back a value from the web server when they open the app that they can skip the login screen.
Besides the links to the docs I provided above, Here is a good example in the docs to get you started
If you are in need of something more robust than what SharedPreferences provides, then look at the Storage Options section of the docs. Hope this helps
Retrieve prefs
SharedPreferences prefs = this.getSharedPreferences("john smith", 0);
saved= (prefs.getString("loginSaved", false));
if (saved)
// do stuff here
else
// require login
SharedPreferences is what you're looking for!
Check out this code to learn more about how to use it:
http://kettiandroidproject.googlecode.com/svn/trunk/Private/Earthquake/src/com/company/earthquake/Preferences.java
I want to be able to prompt the users to enter the details required for the app only when it starts up for the first time (not other times). These details will be held in a database on the phone.
Would it be better to check the database each time or put these details in a shared preference? Furthermore, is this type of activity even possible on Android?
Depends on the details really. If it is a large amount of information you might consider using a Database. For just basic identification information, SharedPreferences should be fine.
If you do use a Database, you might consider loading them into your Application object. This would depend on how frequently you use this information in your app.
To check for the first start of the app, a SharedPreference is generally used AFAIK.
Use SharedPreferences or Database depending on the data complexity and its need later on..
Store a Flag in SharedPreferences and set this when user enters the data.
Check that shared preferences flag while on your first activity and redirects accordingly(to Details/one time page) if required.
here even if a user manually deletes data using Settings > Manage app > your app > clear data, Your app will know to get the details then from user....