What is more efficient? Static, data Passing, shared preferences, Database…?

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.

Leave a Comment