2

For an application i need to store some data to be stored in one activity and then get it in another activity (the activities are not one after another). There might be about 15 data sets- each sets contain id and name. Plus, i don't need the data to persist after user leaves the application.

So, i can think of 2 options-

1) Put each id & name in objects and store the objects in an static array.

2) Save data to Sqlite.

Till now i knew and also was told- not to use static variables until needed too much but someone told me today that saving in Sqlite will just take off more memory and space without any reason; so, rather go for static array of objects.

Now my questions are:

1) which is the best option?

2) How much space does each option need and how do i know it?

A bit explanation will be highly appreciated.

Rhythm Shaon
  • 127
  • 1
  • 6

5 Answers5

1
  1. Storing in SQLite is safe and better as Android is notorious to clean static objects when an application is in background which will cause weird null pointer exceptions when application will resume after a long time.

check this, this, this, and this

  1. SQLite will take space on disc aka ROM and static array will need space in RAM, reading from SQLite will consume some processing power but this will allow you to free up the RAM as soon as you are done with your array for that instance.

Alternatively you can also go for shared preferences if number of records are going to be less.

Community
  • 1
  • 1
Rahul Tiwari
  • 6,851
  • 3
  • 49
  • 78
0

The space for fifteen records does not matter one way or another.

The purpose of a database is to store data, i.e., to ensure that the data is still available after your app has been closed and restarted.

CL.
  • 173,858
  • 17
  • 217
  • 259
0

What is the case? Are you only carrying the values over to another activity, expecting the app to stay active?

Do you want the values to only save if the user uses or saves them, or do you want them to persist once entered?

king_abu1918
  • 284
  • 2
  • 6
0

Storing data with SQLite only makes sense if you want to make the data persistent.

A good way to send data from one activity to another is to use the Bundle:

You have to make your datasets parcelable, Then you can create an ArrayList of datasets and pass it with:

intent.putExtra(KEY, myDatasets); startActivity(intent);

This solution is faster than saving data to SQLite, since you do not need to write data to the disk. You also do not need any object relational mapping for parcelable objects.

When android kills your activity in the backgroud, the Bundle is always correctly saved and restored on activity recreation.

As stated before, static is a rather poor way since it can cause random nullpointer exceptions on activity recreation.

Mike76
  • 899
  • 1
  • 9
  • 31
0

I had this question myself and I found a third option which works fine, As you pointed out too,try to avoid statics because they are just not very object oriented. (read more here about : Why are static variables considered evil? )
in regards to sqlite , I have nthing against it just only sometimes it seems too much for a simple primitive (light data like an integer or array of string or ...). so I suggest to use shared-preferences, a simple solution which enables you to have persistent data for your primitive variables. I suggest to read this answer if you want to save more complicated data like array-list, pictures etc via shared-preferences .

however the process of saving and loading shared-preferences is pretty simple: (following saves and load an int)

    public void SaveInt(String key, int value){
       sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
       SharedPreferences.Editor editor = sharedPreferences.edit();
       editor.putInt(key, value);
       editor.commit();
}
public void LoadInt(){
       sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
       savedValue = sharedPreferences.getInt("key", 0);
}

source for code: https://stackoverflow.com/a/16194745/4173238

Community
  • 1
  • 1
bastami82
  • 5,955
  • 7
  • 33
  • 44