I have one problem. I am creating some kind of heavy object every time when I open an activity. While using the program, this activity can be opened a lot of times, so every time I should wait while some heavy stuff is done. Is there any way to cache this large object into memory and get it from memory while activity is started for the second time. Or there is another way to do this ?
Asked
Active
Viewed 92 times
0
-
You could use a singleton. – Henry Jul 10 '14 at 07:41
-
You better use some service or background work not to slow the ui – An-droid Jul 10 '14 at 07:57
-
Maybe using something like `LruCache`? http://stackoverflow.com/questions/10841470/android-objects-cache – hungr Jul 10 '14 at 08:20
2 Answers
1
Save object as file:
FileOutputStream output = context.openFileOutput("object.bin", Context.MODE_PRIVATE);
ObjectOutputStream outputStream = new ObjectOutputStream(output);
outputStream.writeObject(objectToSave);
outputStream.close();
Load object from file
FileInputStream input = context.openFileInput(fileName);
ObjectInputStream inputStream = new ObjectInputStream(input);
Object obj = inputStream.readObject();
inputStream.close();
ObjectToSave ots;
if(obj instanceof ObjectToSave)
{
its = (ObjectToSave) obj;
}

Matt Twig
- 444
- 3
- 8
0
You can extend Application and store/retrieve arbitrary object(s) from anywhere, anytime.
So your activity will look like this
void onCreate(Bundle bundle) {
GlobalState state = (GlobalState) getApplicationContext();
MyObject obj = state.getMyObject();
if (obj == null) {
obj = createHeavyObject();
state.setMyObject(obj);
}
// your business logic here
}

tsobe
- 179
- 4
- 14