how much space needed to Store SharedPreference in Android? - android

I have one Application which has more then 5 Activity I want to know how much space is needed to store 10 different values to SharedPreference. or I have to user Sqlitequery ?
Simple in One Activity I have 4 Field like FirstName,LastName, Age, City when user Click on th button then new Activity is called and in this activity Full Information is Display, so I can do that in two way. First Way I Store all the Information in the SharedPreference and in Second Activity I get this Stored Values and another way is i only store record id and store it in intent.putextra and get it to another activity and Fire Sqlite Query. so I want to know which one is best for Application .(As per Memory Uses and Speed )

If you do not need persistent storage then you can simply pass the data using intents from one activity to another or maybe even keep some static variables in a separate class say Const.java
If you need persistent storage and if your data is limited, I would always recommend using SP over SQLite which should be used for relational data. DB operations take more time and can hit performance besides there is always chance of DB getting corrupted.

Sqllite is a really good solution if you want to be able to add easily more preferences.
It's a bit heavy to set-up but really powerfull.
http://developer.android.com/guide/topics/data/data-storage.html
For me, database is for storing data, and sharedpreference just app config.

Shared preferenses apereas to be the choise here, it is very good to store simple settings and very fast and flexible.
A database would IMO be overkill here :)

Related

Is it a good idea to store multiple values as SharedPreference?

In my android app I have about 100 places (maximum will be 200). I want to allow the user to mark each place as visited and store this selection.
So the user can mark/unmark that he already visited some places/cities.
Is it a good idea if I store the values as SharedPreference?
My code:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("London", "1");
editor.commit();
and next time when user marks another place then:
editor.putString("Paris", "1");
I am asking due to amount of possible places to be stored there, which will be maximum 200 in my case. I am usually using this kind of storage just to store some settings or so, but I think this is an easy way to store the values in my case too, I don't want to use database or anything similar for storage.
Whether this is a good idea or not depends on your requirements and expectations. If you store the data like this, it will work for sure, but, there will be some limitations:
It might be complicated to show a list of places to the user. If you store some other data to shared preferences you will need a way to distinguish places from other data. In this case you'll probably need to add a prefix to all your keys, such as "place_London", "place_Paris", etc.
You are relying on English key names so you might have issues with localization if you support other languages
It will be much harder to support versioning and scalability. E.g. if later you have an entity called "Place" and it has more information than just a name with a flag, then it will be much harder to keep it in shared preferences. E.g. if at some point you want to add a corresponding country name to all places, what do you do?
I think in this scenario you actually DO want to use database. It will pay off.
SharedPreferences is a key/value way to save data. I think it is not appropriate to save large amount of structured data as you have to define a key for each value you have.
Using SQLite might be a better option for your case.
You should switch to a more reliable way of storing data in storage instead of using SharedPreferences.
Sqlite is good option but if you don't like to write SQL Queries and want a solution where you want to store data in your storage, Realm is an amazing alternative.
Although before implementing please read more on the pros and cons of using realm. You can read more about Realm here :-
realm.io
Realm vs Room vs ObjectBox

Saving data on Android for data visualization

I'm creating an app where I'm going to be storing data that the user enters. Let's say for instance it's someone weight. I have created an SQLite database that is able to store this information. However, I want the user to be able to update the value if the weight changes, but I don't want to lose the previous data. Would I have to create a separate database to store this past information? Or is there a more effective approach? Thanks for your time.

Ways to store variables throughout the life time of android app in phone

I am new to Android. I am building a test application where I need to store a variable's value say, strength of the user. I want to increase or decrease this strength's value whenever user uses the app. And after user closes the app and reopens it on next day, he should find the same value of strength.
One way I can think of is to store a local db in phone and read/write each time into that, since there are hardly 3 to 4 such variables. So db is not a good option I guess.
Other one I thought was to use android.app.Application class but I am not able to get what I want from that. Can we actually do it using android.app.Application? Or then any other method for 3 to 4 variables.
You can use SharedPreferences to store your variable inside shared preferences. For example, set it like:
SharedPreferences sharedPreferences = context.getSharedPreferences("DATA",Context.MODE_PRIVATE);
sharedPreferences.edit().putString("STRENGTH",yourVar).apply();
Then get it out using:
SharedPreferences sharedPreferences = context.getSharedPreferences("DATA",Context.MODE_PRIVATE);
strength = sharedPreferences.getString("STRENGTH",null);
Use SharedPreferences. SharedPreference provides an easy mechanism to persist a value across the life of an app.
Write an XML file in the application directory that the application can read on startup.
Use application class when our app is open read last state and store your data in shared preference, and you are right db is not good choice.
Like you suggested, you can use an SQLite database to store a table regarding the users. Although, you might only have one variable, strength, associated with each user now but if there's potential for more associated values that you want to store regarding the user, I'll recommend using an SQLite database.
Another option you can use is SharedPreferences, it allows you to store key value pairs. So in your case, each strength value can correspond to a username.
If you haven't learned the different storage options for Android, I'll recommend you taking a look at this.
Hope this helps!

ArrayList or SQLite

In terms of storing data in Android, would it be more efficient to use a large ArrayList or setup an SQLite database? I have ~9000 bus stops and stop names (both in String) that I need to store and I was wondering which method would be the quickest or most efficient.
An ArrayList is probably a very bad idea. I assume you want the data to be persistent, so if your user kills your app your data will be lost if you use an ArrayList. A database is persistent(unless the user clears the cache of the app). So I would highly recommend using a database over an ArrayList.
If your data does not change then you could probably have it read on startup and kept in memory while the App runs. For example, having a .json file with all the stops, read it on startup, put the data in a HashMap or something that is easy to use since you will probably lookup stuff. It would probably work fine with ~9000 entries.
If you are going to add or update information about the stops during usage then the SQLite is the better choice though.
1.) Store and retrieve your data from a SQLite DB since you want persistance. And since you say you have 9k+ rows a simple select will give you everything at once and you can easily filter the data as well if you need to
2.) At startup, put all your data into efficient memory structures like HashMaps(or in your case arraylists) and reference them throughout the app. This way you'll only do one time access.
3.) When in doubt build a DB. No harm, more efficient and easier to handle than files

How to save android text entered by the user?

For my newest app it it one that will save multiple EditTexts where the user can enter stuff.Like say if it was a note app,how would i get them to save?
There are multiple ways to store data on Android. Read about data storage.
In your case you might have multiple text records with some metadata (time when created, etc..), so I'd recommend the database.
Check out how to store preferences with android's "SharedPreferences":
http://www.javacodegeeks.com/2011/01/android-quick-preferences-tutorial.html
This way you can indefinitely store whatever you're working with as a string, object, etc. Also, you can check out how to work with android's SQLite Database, which might be harder to figure out when you start working with it, but it gets easier over time.

Categories

Resources