5

I'm using bitmap in a fragment, and I call bitmap.recycle() in the onDestroy() method of the fragment. But quite a few times I get an exception

E/AndroidRuntime(4869): java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@40659750

I also tried doing this in the onDestroyView() method of the fragment but still got the same exception.

Can anyone suggest what is the best stage of the fragment life-cycle where I should call bitmap.recycle()

pankajagarwal
  • 13,462
  • 14
  • 54
  • 65
  • refer this: http://stackoverflow.com/questions/4959485/bitmap-bitmap-recycle-weakreference-and-garbage-collector – Padma Kumar Dec 19 '11 at 10:29

2 Answers2

0

you can recycle either in onDestroyView or onDestory, but make sure that you are creating your bitmap again in your oncreate or onCreateView.. as you are using the same bitmap reference again it is throwing recycled exception. So you need to create the bitmap again when you are creating the fragment or you can also check isRecycled() to know whether your bitmap is already recycled

Arun
  • 1,658
  • 1
  • 14
  • 21
0

I found the solution for this, i was struggling with exactly the same problem

What i did was i removed all the child views of the content view of the/each fragment. After all the childviews are gone of each fragment, i popped the fragment(s)

I recycled the bitmaps on the 'OnDestroy' of each fragment just like you are doing right now.

Jdd
  • 1