Temporary data storage in android? - android

I want to get information from database only one time per day instead of hitting every time and i have to maintain that information like session.Currently i have planned to use Shared Preferences concept.Is it preparable?

I think the option Sharedprefence is the best. But the valuse will be cleared when clearing the app data!
so include some code to check the existance of sharedpreference values. if not existing, then restore them back by collecting data from your DB.. so you have to hit your DB rarely as user does it rarely!

Related

How long does SharedPreferences save data?

I am making an Android app, and I will do some process at the first launch of the app.After the first launch I will not these process again. So, for doing this, I want to use SharedPrefences.
How long does it save data? When and how does its saved datas remove?
Data in SharedPrefences is saved until the user uninstalls the app, or clears the app cache
Data in SharedPrefences is saved as file in your app internal storage, its be removed in case:
unInstall app
clear you app data
call yourSharedPreferences.edit().clear().apply()
hope this helps
When you clear the cash memory form settings all the information will be deleted and start fully new. so it not exactly that you need to uninstall your app.
So sharedpreferences is basically store data in primitive way which mean it doesn't contain data directly instead it's reference.

Only read sqllite database once when it's updated, not every time when i start the application

I want to read the database when it's updated not every time when i start the android application.
I'm using sqlLite as my backend and CSV file contains Geolocation of the users so it's take a time to read and load the entire database..I want to reduce that time.
Use a flag in shared preferences and set it every time you update data in your database and reset it every time you completely red your database. Every time you open your app check for this flag and if it is set then read database.
See this to know about shared preferences
Android Shared Preferences

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!

Use database for session management

Can I use a column in table to check if user has logged in.
When user logs in I set value to 1. When he logs out I set to 0.I dont want to use SharedPreferences. Is using it like this inefficient.
I used sharedpreferences first. I set username in the sharedpreference along with other preferences and display username in the nav drawer. When I install the app in another device, I think even the sharedpreference file will be installed and I saw that in the new device the username is displayed in the from the sharedpreference file though that user doesn't exist in the table in that device's database
When user logs in I set value to 1. When he logs out I set to 0
Here, what will happen is when you set values to 0 or 1 you have to open and close database everytime and will be a hastle to maintain. you have to check it each time what flag is. while in SharedPreferences it will be globally accesible and easy to set flag.
I would suggest you to use SharedPreferences because
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.
To give an example, SharedPreferences are useful for storing user preferences, where there are just a handful of variables that need storing. SQLite on the other hand would be better for storing data where there is a large set of items, such as song titles in a music library which need to be searched through
Check SharedPreferences and SQLite.
Better to use Shared Preferences instead of using Database for such a small thing.
Refer this : http://www.androidhive.info/2012/08/android-session-management-using-shared-preferences/

How to keep stored data even when an application update is done?

My question is simple :
I have a "flag" stored in SharredPreference but when the user is doing an application update, this data is ereased. Is there a way to keep data stored even if the user is doing an application update ?
Well, as far as I know you have only two choices - write it in a file or save it in the database. I always put the data in the db, from witch they are loaded at program start. Just have a table with the prefs in it and you are sure, that your data is not deleted, even after updating. The database interface is easy to use and lean - therefor I use it.

Categories

Resources