Which one is faster to access frequently used data in android? - android

I need to use few variables frequently but I want know that which one will give fast response SQLite Database or Shared Preferences ?

Shared Preferences:
What it is?
Shared Preferences is an API from Android SDK to store and retrieve application preferences. Shared Preferences are simply sets of data values that stored persistently. Persistently which mean data you stored in the Shared Preferences are still exist even if you stop the application or turn off the device. SharedPreferences available at the Activity level or shared across all Activity in application package.
When to use Shared Preferences?
o You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed).
o Think of a situation where you want to save a small value (Login Credential) that would be referred later sometime when user launches the application. Then shared preference comes into action
How to Use?
SharedPreferences sh_Pref = getSharedPreferences("Login Credentials", MODE_PRIVATE);
Editor toEdit = sh_Pref.edit();
toEdit.putString("Username", "Name");
toEdit.putString("Password", " Password");
toEdit.commit();
SQLite Database:
Android provides several ways to store user and app data. SQLite is one way of storing user data. SQLite is a very light weight database which comes with Android OS.
How to use ?
To use SQLite in Android, a java class should be created as a sub class of SQLiteOpenHelper. This class will act as a database controller which will have the methods to perform the CRUD operations. This custom java class should override the methods named onCreate() and onUpgrade().
onCreate() method will be called for the first time when the Android application is run. First the database instance should be created using the method like getReadableDatabase() or getWritableDatabase() based on the type of access required. Android supports this method by providing in-built methods. For that, SQLiteDatabase class should be used.

Related

getPreference() - accessibility of a shared preference for components of the application

In the official site of Android, for getPreference() method, it is said that :
Retrieve a SharedPreferences object for accessing preferences that are
private to this activity.
And here(How do I get the SharedPreferences from a PreferenceActivity in Android?) it is said that :
These preferences can only be used within the particular activity and
can not be used by other components of the application.
However, in url http://skillgun.com/android/basics/interview-questions-and-answers/paper/25 5th question,
it is said that it is not guaranteed to be protected as it will be
stored with the name of Activity.
I am confused whether other components of an application(such as activity,service etc.) can access the shared preference created by getPreference() method. is a shared preference created by calling getPreference() method accessible only for an activity for all circumstances?
Basically if you use shared preferences you will be able to read and write the preference from any part of your app. But other apps will not be able to access this information.
The statement regarding not being protected refers to the fact that rooted users (and apps) can read this files from the phone internal storage. So avoid at any cost saving sensitive user information in Shared Preferences. Ex. Dont store userNames, Passwords, personal details, etc.
Use shared preferences for simple things you wish to store Ex. If developing a contacts app you can store whether the user likes to read his contacts firstName LastName or LastName FirstName.
This kind of data is very short and not compromising.
If you require to store sensitive information always encrypt the data first.

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!

how to use session in android

I read shared preference is used as a session for small data storage. I would like to know , what are the way to manage session in android and which is more efficient and useful way to use session. please guide me.
It can be as simple as storing a datetime string lastUsedDate in SharedPreferences, and looking up lastUsedDate everytime the app's onCreate() is called and calculate the time passed since.
you can manage your session via api, you can store a value in shared preference when your app is lunched. This start a session when you store a value , when you need to close this session or log out just change your share preference value and update your api and your api return a value which define your app state
you can use your sqlite to store sessions..you can also make use of Bundle.
Before you can talk about the one most efficient in storing session, the data size will have to be considered.
Your data storage options are the following:
Shared Preferences
This stores private primitive data in key-value pairs.
Internal Storage
This stores private data on the device memory.
External Storage
This stores public data on the shared external storage.
SQLite Databases
This stores structured data in a private database.
Network Connection
This stores data on the web with your own network server.
you can also check here for more explanation

Android onSaveInstanceState for all instances of activity

After reading the documention it seems that onSaveInstanceStaate works per instance (as the name suggests). I am just wondering what the preffered method of storing data is so that it is available for all instances of that activity?
As MaciejGorski mentioned in his comment, there are different levels of data storage available in Android:
Shared preferences
Internal storage
External storage
SQLite database
Network
From personal experience, the lower you go down this list, the more complicated your implementation will become. Thus, if you are simply trying to save simple data for your app to be shared among different instances of an activity (or of multiple activities), shared preferences are certainly the way to go. You can even create private shared preferences, which only your app can access.
In any case, check out this SO answer for how to implement them: How to use SharedPreferences in Android to store, fetch and edit values

Difference between shared preference and sqlite

I know this topic has been discussed before on Stack Overflow. But there are still some things that are not clear when I read previous posts about it. So here they are:
I know that we use shared preference for small datasets and sqlite for large data manipulation, so if we just want to save a username and password should we use shared preferences?
Won't shared preferences be lost when user uninstalls the app? For example I download an app called abc and save my username and password. Then I uninstall this app from one phone and try to access it from other phone using the same username and password. Will this be saved using shared preferences or the data be lost?
What are the main reason we use one over the other beside large and small datasets?
You can think of the difference between shared preferences and an SQLite database in terms of data size but that isn't entirely accurate. A better way to think of it is in terms of the structure of the data you want to store.
Shared preferences can only store key-value pairings whilst an SQLite database is much more flexible. So shared preferences are particularly useful for storing user preferences, e.g. should the app display notifications etc. Whilst an SQLite database is useful for just about anything.
Both data sources are local but something you should be aware of is the ability to backup your application data to cloud storage that is linked to the user's Google account. This makes it much easier for your users to change devices and for their applications to easily transfer to the new device. For more info take a look here.
In the situation you described about you will lose the user name and password in both situations. The data is stored on the phone, when you uninstall the application, the data that some with it will also be lost. The user will have to re-enter this information.
You can save the user name and pass in either the shared Preferences or a DB, that is personal preference. Just make sure you lock either down, i.e. don't share the DB or Shared Preferences that you keep this information in.
As for the difference... shared Preferences should hold well... shared Preferences... here is an example:
If I create an option to change the background color, I will store all available options in a DB that can be loaded into a adapter view for the user to choose from. But I will store the color that they have selected in the Shared Preferences. This way when the application load I can get the Shared Preference value of the background color that should be used.
SharedPreferences is used for just that, storing user preferences shared application-wide. You can use it, for example, to store a user's username, or perhaps some options he or she has configured in your app in which you want to remember.
SQLite is a relational database. It's used to store your application's data, not preferences or configuration information.
Both are stored locally on the device.
1.SharedPreferences stores only Boolean, int, float, long, String five kinds of simple data types, such as can not be conditional query. So, whether SharedPreferences data storage operation is how simple it can only be a supplement of storage, but can not completely replace other data such as the SQLite database is stored.
2.SharedPreferences based on the XML file to store key-value key used to store configuration information(mainly user preference for your application).
3.Sharedprefrece just like cookies in web which store some basic information at client side.
both store their data locally, so uninstalling the app will delete both. other than that, SharedPreferences is easier to program, and you're right about the data amounts.
In general, shared preferences should be used if you want to allow your user to directly manipulate certain data fields. Shared preferences are basically user preferences; if you would like the user to reconfigure the app to behave in different ways, you should expose that functionality as a shared preference. On the other hand, the SQLite database should be used if you want to limit the visibility of the data to just the application, if you want a stronger guarantee that the data be persistent, and if you want the application to behave independently of what is stored in the database. Of course, you can use both in one application.
Shared preferences and the database are part of local data that the application stores. If you uninstall the application, both of the data stores will be removed.

Categories

Resources