When I log a user in, I pull down some user data. It's just a userid and stuff like that. A couple of strings really. The thing is I don't want to pull this down every time if it's the same user over and over and so I want to store it locally across sessions. I'm aware of SQLite but is making a table just for 1 row really the best solution? Is there not another, better way?
Check the developer.android.com site. Data Storage :
http://developer.android.com/guide/topics/data/data-storage.html#pref
The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types
If primitive types is all you need, and the boundary is your application (no multi process stuff), then you should be OK with just using SharedPrefs. Anything else (files,sqlLite) is overkill. SharedPrefs has a clean api that should be sufficient for your needs.
See the developer.android.com site for shared pref usage.
You could use Shared Preferences. It is meant for that.
This other question asks about storing files on the device. The recommendation is to store files on the SD card (if available) and that could be a solution to your problem.
Related
I want to store multiple values (String, Int and Date) in a file via Java in Android Studio.
I don't have that much experience in that area, so I tried to google a bit, but I didn't get the solution, which I've been looking for. So, maybe you can recommend me something?
What I've tried so far:
Android offers a SharedPreferences feature, which allows a user to save a primitive value for a key. But I have multiple values for a key, so that won't work for me.
Another option is saving data on an external storage medium as file. As far as good. But I want to keep the filesize at minimum and load the file as fast as possible. That's the place, where I can't get ahead. If I directly save all values as simple text, I would need to parse the .txt file per hand to load the data which will take time for multiple entries.
Is there a possibility to save multiple entries with multiple values for a particular key in an efficient way?
No need to reinvent a bicycle. Most probably the best option for your case is using the databases. Look into Sqlite or Realm.
You don’t divulge enough details about your data structure or volume, so it is difficult to give a specific solution.
Generally speaking, you have these three choices.
Serialize a collection
I have multiple values for a key
You could use a Map with a List or Set as its value. This has been discussed countless times on Stack Overflow.
Then use Serialization to write and read to storage.
Text file
Write a text file.
Use Tab-delimited or CSV format if appropriate. I suggest using the Apache Commons CSV library for that.
Database
If you have much data, or concurrency issues with multiple threads, use a database such as the H2 Database Engine.
I would like to implement a persistent queue in Android. Essentially a queue where data is stored until another thread takes it and sends it somewhere. The order in which data is stored or consumed is not important (ex. FIFO, LIFO, LILO, etc). Is there a modern library in Android that supports this?
I found a previous question on SOF but this dates from 2012: How to realize a persistent queue on Android
There are a lot of way to do this. You can persist the collection by saving it into a database like SQLite, serialize it as a file and deserialize it when you need it, or you can save it in SharedPreferences of Android.
Anyway there is a cool library called Hawk
(https://github.com/orhanobut/hawk)
that is a
Secure, simple key-value storage for android
How Hawk works:
If you take a look at the Hawk source code you can see that it uses SharedPreferences to save data to the disk. It serializes the object you want to persist and saves it into SharedPreferences as a String.
Quotes and images are taken from Hawk github page.
You can find other alternatives here (in the "Persistence" section) : https://github.com/codepath/android_guides/wiki/Must-Have-Libraries
You can check out Priority Jobqueue
It has an option to persist the jobs and prioritise them.
I'm developing a little game for android and i'm at the point where i want to save data of the progress the user has made so far.
I read about sharedpreferences, SQLlite... i have used some XML parsers aswell but my question is, if i want this:
Fast performance.
Secure or encryptable.
I want to point out that the data to save is not very long, profile name, levels completed, faction selected...
Thanks in advance!
Any solution will be "fast enough" for loading/saving the data... and any of them will be encryptable (as in they can all store string data).
SharedPreferences would probably be the simplest way to go. Perhaps store your data as JSON, then store the entire JSON string in SharedPreferences. Then you can easily add encryption to it later.
I'm building a home replacement app. I need to store the ArrayList with the apps the user picked to show on the launcher in the internal memory. I mean the array mustn't be deleted when the app is closed.
I'm very close to finishing the app and I don't think I'll work a lot more on Java, I'm not a programmer so I just want the easiest way to do it. How can I store and retrieve an ArrayList in the internal memory?
Use SharedPreferenes to store and retrieve the arrayList..
Check this link..
Save ArrayList to SharedPreferences
You should store such data as Arraylists in the database and when you relaunch the app just fetch the data from database(SQLite) and display it to the user.
Heres a good tutorial on android SQLite.
Take a look at this link, for all the different data storage mechanisms in android. But for your requirement I would suggest using a db.
The easiest way is to use Java serialsation. It writes the content of an object to a (file-) outputstream. Thats a few lines of code.
a bit better (faster, less bytes, readable by other non java systems) is to uses a custom serialisation, using DataOuputStream. That creates a binary file in the format you define.
I've made an Android application which contains most used German words and sentences. The application contains a CSV file which stores all the data.
Currently it is working as expected but I want to ask if there is a better way to store such data directly in the app?
I'm also thinking about the ability to update the data via internet like adding new words and sentences.
Thanks!
Miretz
If you want to modify the content (update, remove etc.) I would suggest using SQLite DB which has a pretty nice built-in integration with the Android platform.
There are 2 types SQLDatabaseLite and SharedPreference. Major difference between both is that one is organized and the other not so.
If you need a quick use of a storage facility within your app for example changing text sizes between activity SharedPrefference works best for you.
If you have a complex database system where you need more than one data to be saved for a particular event SQLDatabaseLite is for you example of this is spreadsheet of data for customers; Name, Phone Number, etc.