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.
Share
My opinions:
Each one is best depends on the scenario. Let me explain.
Static Variable:
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.
Passing data through Intent:
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:
Shared Preferences is nothing but a simple table which has two columns. (key, value).
advantages:
cons:
Database:
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.