简体   繁体   中英

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 being 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 .

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM