Is it possible to recycle all data at OnStop() AND use a retainer Fragment?

143 Views Asked by At

1)It is considered a good tactic to recycle all bitmaps and data at activity's OnStop method.

2)It's also considered a good tactic to use a retainer Fragment to avoid recreating data at every configuration change. But I don't see how these two can be combined?

Let's say I use a fragment to load a bunch of bitmaps...At OnCreate I check if that Fragment is null or not to get it's data or to instantiate a new one to create them. If i recycle all my bitmaps at OnStop() then there will be nothing left retrieve at the configuration change cause all data will have been recycled.

So....I don't see any way to combine these two tactics. Am I wrong? And if not which of the two is best to use?

My case is about loading images from SD card folder. could be only one pic, could be 500... and showing pictues isn't all my app does so after this activity there could a need for memory by some other activity.

1

There are 1 best solutions below

0
On

From Managing Bitmap Memory:

On Android 2.3.3 (API level 10) and lower, using recycle() is recommended. If you're displaying large amounts of bitmap data in your app, you're likely to run into OutOfMemoryError errors. The recycle() method allows an app to reclaim memory as soon as possible.

According to this you don't even need to call recycle on devices running API 11 or higher, so it may not really be a problem for you.

You also really don't need to recycle bitmaps if the app is being destroyed as the system is going to reclaim all memory the app is taking up to begin with.

Recycle is only needed if you are showing a massive amount of bitmaps or large bitmaps and need the memory reclaimed in your app while it's still running.

Another thing to note is with the strategy you're trying, you wouldn't clean resources in the Activity's onStop() but rather the retained Fragment's onDestroy(). OnDestroy() on a retained fragment won't be called on configuration change because the Fragment isn't ever being destroyed. Thus, your resources can stay in memory beyond your Activity's lifecycle and will be destroyed at the end of your Application's lifecycle.