I was recently doing an application in android and at some part I got a doubt that which is the efficient way of sharing Data. Like I can declare a static variable in one class and call that variable from the other class using Classname.Variablename or I could pass that data with my intent and get data from intent from the other class or I can use shared preferences and get data from it or I could even store that data in database and retrieve it from the other class from the database. What my doubt was which will be the most efficient way to do that (get data)? Since android applications are ultimately designed for phones memory usage and efficiency should be constraints. Could anyone guide me in the right path, it would be very helpful.
Shared preferences allow you to store small amounts of primitive data as key/value pairs in a file on the device.
Shared Preferences allow you to save and retrieve data in the form of key,value pair. In order to use shared preferences, you have to call a method getSharedPreferences() that returns a SharedPreference instance pointing to the file that contains the values of preferences.
With SQLite you use SQL language to create tables and then do insert, delete, update operations just like in any other database. But in case of shared preference you use just normal APIs provided by Android to write, read and update these values to a XML file.
There's no hard limit. The main reason it is not recommended to use SharedPreferences in place of the database is mainly the performance -> shared preferences data is keept in ordinary flat XML file which lacks all the features SQLite offers.
My opinions:
Each one is best depends on the scenario. Let me explain.
Static variable are common ways to give access to whole application context. If you want to maintain few datas which is not necessary to be maintained after the application exited, this is the best one.
This is not a kind of data storing. This is a kind of data sharing. I think this is the best way to pass data from activity to activity. But maintaining the keys for values in Constants is a good programming thing.
Shared Preferences is nothing but a simple table which has two columns. (key, value).
advantages:
cons:
When we have a lot of values to store with complex structure, we are left with only one great solution, ie. DB.
advantages
cons
So we found the major problem is we can't maintain the persistence whatever storage we use. A simple and effective solution is to maintain data online and sync it with mobile db each time the application is started.
I may be wrong, but i feel like i have to share my knowledge/experience... well the fastest and easiest way is to use static... but i will not recommend this.
My Selection would be: Database
Reasons:
1- By declaring static variable, it is not reliable
, as at some stage, when your applications or some other application is taking too much memory, GC will try to collect all unreference objects, static is a good candidate here... like Activity A has static variables and you are currently in Activity B, now gc will remove A and its objects, there is a chance it will also collect static. Now selecting database is obvious here, i dont need to tell you because, within ur currect activity you can always retrieve data from database.
2- By declaring static variable, if its just a int or small datatype, thats not a big deal, but what if you are going to save a big list of complex structures, then there is a very high chance that your activity will leak memory
which is also going to give u troubles. No leak if you properly use database.
SharedPreference/intent
is also good/fast/memory effecient(i guess), but can get a little messy when passing large complex structure.
So to sum up, If you just want to pass a little structure (string + int) and your application isn't touching the limits of heap then u can use static
but if you are doing some memory intense calls and passing somewhat large data then database
is reliable, can be little slower than static but fast enough that we wont notice.
Hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With