0

I have been struggling with bitmap memory management when transitioning from one activity to another. I have found the two biggest helps are: (1) keep track of all the bitmaps in the fragment or activity in an array and then nulling the array at onPause() and (2) calling finish() on my current activity when I transition to another activity.

But with finish() I lose all my previous state information.

Does anyone know what finish() is doing for my bitmap memory management that nulling my bitmaps would not do?

I would like to manually do whatever it is that finish() is doing to clear up my memory. Here is my "clearBitmap" code I use to null the bitmaps:

public void onPause()  {
    super.onPause();
    clearBitmaps();
    System.gc();        
}  

public void clearBitmaps()  {       
    if (bitmapArray!=null) {
        for (int i=0; i<bitmapArray.length; i++)  {
            if (bitmapArray[i]!=null)  {
                bitmapArray[i].recycle();
                bitmapArray[i] = null;  
            }                           
        }
        bitmapArray = null;
    }       
}

I tried calling the clearBitmap() method in lieu of finish() but it didn't do any good.

By the way, I am efficiently loading the bitmaps and doing all the other tricks that Android tells me to do for Bitmap management, but still have this problem. I find this issue baffling because I am not using a lot of bitmaps. But if I transition back and forth between activities about 40 times it always crashes (it used to be 6 times, so I have made significant progress with the methods above).

Thanks!

4

1 回答 1

0

这并不是我自己问题的真正答案,但我最终调用 onBackPressed() 来处理内存管理问题并保留我的状态信息。

于 2013-03-19T02:54:56.120 回答