Android: Resuming an activity without reloading
I have two activities, say Activity A
and Activity B
.
Activity A
is created when the application starts and then Activity B
is being called from Activity A
as follows:
Intent i = new Intent(A.this, B.class);
startActivity(i);
Activity B
loads a Thread
on its onCreate()
and then when you click on Back
, it will return to Activity A
. This Thread
is loading some images from a Database
that is bein开发者_如何学JAVAg updated within Activity B
as well.
How can I save the state of Activity B
so that I can avoid reloading the Thread
that I have in Activity B
when I call it again from Activity A
.
Create a singleton class which loads the images and caches them. Every time you launch Activity B
check this singleton class for images. If not loaded then load them otherwise read from the cache
Move the database loading to a separate class and load from there.
You may want to check this question: Saving Android Activity state using Save Instance State
But consider that, depending on how long the Activity A takes and how much memory the device has left, Activity B could be killed before it returns.
精彩评论