Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
220 views
in Technique[技术] by (71.8m points)

android - What is more efficient? Static, data Passing, shared preferences, Database...?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...