I am new in Android.
I have a database like this.
I used SharedPreferences for remember user information.
However, there is a point that I do not understand. When the user logins the application, do I need to save all Firebase data in SharedPreference for the application to remember the user?
Which type of datas save in SharedPreference? I just want, If a user logins the application, my application will remember the user and its checkboxes, comments, likes or something else.
It might be a stupid question, but I am new in this. Thank you for all.
Yes you can use sharedPreferences for this purpose. You can save int, String, boolean, float, double... in sharedPreferences.
You need to save user information in sharedPreferences on login.
You can use SharedPreferences to save values. Make a model and the using Gson save all the values in one string instead of saving seperately.
Related
I'm a beginner developer and I'm currently developing a Quiz app.
Each Quiz contains 50 questions and I'm storing them using Room database.
I've got Coins, Stars, and the score and number of finished questions for each Quiz (And also user's information) that I want to save.
And I still don't know what is the best method to do that.
I was thinking about combining SharedPreferences and Firestore to save user's progress and information, SharedPreferences to save them locally, and Firestore so that when the user signs in on another device, all his progress and information will be retrieved (and saved in SharedPref).
(**Data will be saved in Firestore when the user only signs in with Google)
Now my Questions is :
Can I combine SharedPref and Firestore when saving data? and is it a good idea to do so?
if not, should I only use Firestore for that?
Can I combine SharedPref and Firestore when saving data?
Yes, you can but I cannot see any benefit at all. You say:
Firestore so that when the user signs in on another device.
To have the data available no matter what device the user is using means that each time the user closes the app, you should save the progress in Firestore. So I cannot see why would you use another data structure. Besides that, SharedPreferences do not persist along with app uninstalls. SharedPreferences data is always deleted.
It would have been an excellent idea only if you wanted to store the progress in SharedPreferences and commit the quiz to Firestore only when it's finished. In this way, you'll only be billed with a single write operation, which sounds perfect. However, this solution doesn't provide the feature to have the progress available, no matter what the device the user uses, as it's stored only on a single device.
Firestore has a feature where it will automatically store data in the device's cache, so when your user go offline, they can still view what they last had access to. Since firebase already takes care of when your users go offline, I suggest going with firestore only route.
You can read more about firestore's offline cache here(Watch the video, it's great):
https://firebase.google.com/docs/firestore/manage-data/enable-offline
I have an app where anyone who downloads the app can add data to the database. The data THEY added will get displayed in a listView for only them to see. The users don't have to register an account or anything. Now when multiple people use the app, different data gets added to the database. So my question is, what are good ways to differentiate data, so the person who added data, only sees the data they added.
I have two ideas, either add the data ID to sharedpreferences, so when I select the data from the database, I select data where the data ID equals to the one in sharedpreferences OR when I add the data to the database, I add a unique key, so I can select * where unique key equals x.
I like the second idea more, but what would be a good unique key to use. I've thought of using
private String android_id = Secure.getString(getContext().getContentResolver(),
Secure.ANDROID_ID);
but is that a reliable solution, are there any other unique key solutions I could use?
Any feedback is much appreciated!
I think that the best way to handle such cases is to generate a UUID for each user.
Android itself will help you generate one.
You can generate the UUID the first time the app is launched, and store id in SharedPreferences.
I am making an app where as soon as you sign in, it will query into the database and retrieve your details, but what's the best way of storing these details as i change between activities without having to repeatedly query into the database again to retrieve them ?
I have tried using the intent.putExtra() method but it's causes NullPointerExceptions when i return to previous activities. Also putExtra() will not work on one of my classes which is HostApduService.
So what's the best method to maintian a session ? Will i need to use cookies or something ?
SharedPreferences is what you are looking for.
A SharedPreferences object points to a file containing key-value pairs and provides simple methods to read and write them. Each SharedPreferences file is managed by the framework and can be private or shared.
How to use it:
Read data:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int value = sharedPref.getString("my_value_key", "default_value");
Write data:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
sharedPref.edit().putString("my_value_key", "my_value").apply();
You can read more about SharedPreferences here.
Good luck. :)
I have an android app with user login i need to store the login details in my app.
My app also has some user history of certain request made by him/her.
My app is a software service app so user details must be stored in app which is fetched from the database
Is Sqlite or sharedpreference suitable for my application?
Depends, If you only want to save sessions of user use SharedPrefrence or else if the data is more you can use sqlite
/**
* method to set the login status for the application
*
* #param context
* #param status
*/
public static void setLoginStatus(Context context, int status) {
sharedPreferences = context.getSharedPreferences(PREFERENCE_NAME, Activity.MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
editor.putInt(KEY_LOGIN_STATUS, status);
editor.apply();
}
NOTE :
About that its totally on your choice what you use to save the current
state of user , i will prefer using shared preferences. And for signup
and sign in use SQLite database. Shared preferences are commonly used
to perform light operations. You can search through data in SQLite as
you may have many users.
So use SQLite to register and login and then use shared preferences to
save tha current state of user.
Sqlite is always a better option to store the data there you can manage in proper format and you can encrypt as well.
I would suggest, you should use sharedPreferences for saving the login details and i suppose whatever request you are storing, contains more data, so save your requests in SQLite.
Here is some good details about theme :
Pros and Cons of SQLite and Shared Preferences
SQLite
Large amounts of same structured data should be stored in a SQLite
database as databases are designed for this kind of data. As the data
is structured and managed by the database, it can be queried to get a
sub set of the data which matches certain criteria using a query
language like SQL. This makes it possible to search in the data. Of
course managing and searching large sets of data influences the
performance so reading data from a database can be slower than reading
data from SharedPreferences.
SharedPreferences
SharedPreferences is a key/value store where you can save a data under
certain key. To read the data from the store you have to know the key
of the data. This makes reading the data very easy. But as easy as it
is to store a small amount of data as difficult it is to store and
read large structured data as you need to define key for every single
data, furthermore you cannot really search within the data except you
have a certain concept for naming the keys.
According to the Size or Format of your data you can choose one, for login information I suggest using SharedPreferences
I have been developing Android application which should have following function: it should store some data between Application executings; also if I delete application and re-install it app should restore values of data. I knew that I can store data into SharedPreferences (for working with data in my program) and serialize into file in onDestroy() event. But SharedPreferences is Non-Serializable class and I can't use it. Please, suggest me another way for my task, or tell me how I can serialize SharedPreferences. I know that I can just write important data into simple file but may be there is another means for my problem? Thank you
You're not going to be able to store data locally after you delete an app, except if you put the data on the SD card, but at that point everyone can read/write/delete it.
Check this out for more information on data storage:
http://developer.android.com/guide/topics/data/data-storage.html#filesExternal
Usually you would store your settings in an SQLITE db or in some sort of http://developer.android.com/reference/java/util/Properties.html
Cheers