1

I have an app in which I have more than 450 custom Java objects, and about 200 other variables of strings, int, etc. I know that these much variables use large RAM of the device, so I want to know the best way for storing and accessing them. Some variables are used in many activities.

Zoe stands with Ukraine
  • 25,310
  • 18
  • 114
  • 149
Ismaeel Sherif
  • 395
  • 5
  • 14

2 Answers2

1

I would definitely persist them to a a file since they are so many. Doing this you can load the classes in when you need them and not worry about memory

Saving custom class

FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(this);
os.close();
fos.close();

Loading custom Class

FileInputStream fis = context.openFileInput(fileName);
ObjectInputStream is = new ObjectInputStream(fis);
SimpleClass simpleClass = (SimpleClass) is.readObject();
is.close();
fis.close();

Here´s another question with some cool implementations of this

Erik
  • 4,819
  • 10
  • 60
  • 116
0

you can use data base for storing variables in memory but it better to use Sharedprefrences to store all variables :

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); 
Editor editor = pref.edit();
editor.putBoolean("key_name", true); 
editor.commit();

and easilly get the variable :

pref.getString("key_name", null);