17

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.

SKT
  • 1,821
  • 1
  • 20
  • 32

4 Answers4

27

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:

  1. Fast retrieval
  2. Easy to understand and program

cons:

  1. Maintaining Keys are difficult if we store a lot of values.
  2. User can clear this at any time.

Database:

When we have a lot of values to store with complex structure, we are left with only one great solution, ie. DB.

advantages

  1. We can maintain structure of data.
  2. Android has nice and simple APIs to handle sqlite operations.

cons

  1. Operation is little bit slow comparing to shared preferences.
  2. user can clear this at any time.

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.

naught101
  • 18,687
  • 19
  • 90
  • 138
Sadeshkumar Periyasamy
  • 4,848
  • 1
  • 26
  • 31
  • 4
    what are your sources for affirming sqlite operations are slow comparing to shared pref? – njzk2 May 17 '13 at 11:48
5

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.

Farhan
  • 13,290
  • 2
  • 33
  • 59
  • 2
    Regarding your first point, I don't believe statics are garbage collected. See http://stackoverflow.com/questions/453023/are-static-fields-open-for-garbage-collection. It's still a bad idea to use statics because they'll be gone when the process is killed. – gngr44 Mar 02 '12 at 08:19
  • well, i said in the start, that i may be wrong... and very much open for comments.. and it was based on my experience, what if your heap usage is getting too high that gc will collect all the activities and all the data except for the one u currently present in... this is what i m trying to say. – Farhan Mar 02 '12 at 08:29
  • 1
    @Farhan Thank you for letting me know about the chance of losing static variable value if there are too much activities in the stack. +1 for it. But I'm accepting SadeshKumar's answer for it give some more information. – SKT Mar 02 '12 at 08:56
  • yeah no prob buddy, upvoting is enough for me as it tells me m quite right... :) – Farhan Mar 02 '12 at 09:12
3

As per my point of view,

It depends what kind of data & how important it is. You can use them in a following way,

  • Static : when you have limited use till the application is running.
  • SharedPreferenced : when data size is very small but important for future use.
  • Database : when you have large size of data.
Lucifer
  • 29,392
  • 25
  • 90
  • 143
0

Don't go for static variables .It would be null , when you return back to activity .

See this answer for more details

Community
  • 1
  • 1
Don Chakkappan
  • 7,397
  • 5
  • 44
  • 59