Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using shared preferences vs. bundle in Android

Tags:

android

The app I'm making retrieves between 1 and 2 dozen records from a table in a DB so that certain pieces of data can be used during the app (the table has 6 fields). It won't need to be stored permanently on the device. Is shared preferences used for something like this or should I be utilizing a bundle?

like image 623
acl77 Avatar asked Dec 28 '25 17:12

acl77


2 Answers

You can just query the db and get the data you want and store it in a normal fashion in variables.

SharedPreferences is mainly used when you want to retain data between different app executions.

Bundle is used when you want to safely transfer data between activities or between an activity and a fragment.

So unless you have some such requirements, you can just store them in normal variables.

like image 164
SoulRayder Avatar answered Dec 31 '25 09:12

SoulRayder


If you want to access to same data from different activities and also if you are not going to store them permanently, you can use static variables in a class of your app. So;

// create static variable in a class

class SomeClassName {

    public static ArrayList<MyObject> myObjects;

}

init the list when you get it from DB

SomeClassName.myObjects = GET_ITEMS_AND_REFERENCE_THEM;

then use it in another class like this

MyObject myObject = SomeClassName.myObjects.get(0);
like image 41
eluleci Avatar answered Dec 31 '25 07:12

eluleci