Hi,

This would be hard to explain so I'll try to make it as simple as it
can be.

I have an app that consist 2 activities. In the first one I have 2
animations with bitmaps.When I used the google suggested approach for
animationdrawable (with xml file) I was constantly having OOME. Only
using Bitmaps which I can release manually solved the problem, but not
quite. For clarity I'll describe this in points:

1. On launch I prepare everything, load the first animation and start
a timer:

                        analyseBitmaps[0] = BitmapFactory.decodeResource(res,
R.drawable.analyse_frame_0);
                               analyseBitmaps[1] = 
BitmapFactory.decodeResource(res,
R.drawable.analyse_frame_1);
                               analyseBitmaps[2] = 
BitmapFactory.decodeResource(res,
R.drawable.analyse_frame_2);
                               analyseBitmaps[3] = 
BitmapFactory.decodeResource(res,
R.drawable.analyse_frame_3);
                               analyseBitmaps[4] = 
BitmapFactory.decodeResource(res,
R.drawable.analyse_frame_4);
                               analyseBitmaps[5] = 
BitmapFactory.decodeResource(res,
R.drawable.analyse_frame_5);
                               analyseBitmaps[6] = 
BitmapFactory.decodeResource(res,
R.drawable.analyse_frame_6);

                               for (int i =0 ; i< analyseBitmaps.length ; i++)
                               {
                                   analyseAnimation.addFrame(new
BitmapDrawable( analyseBitmaps[i] ), 50);
                               }
                               analyseAnimation.setOneShot(false);
                               
animationImage.setBackgroundDrawable(analyseAnimation);
                               analyseAnimation.start();


                MyCount counter = new MyCount(15000,1000);
               counter.start();

2. Then after the timer comes to an end I release the first animation
bitmaps and load the second one in an identical way. Then I do some
long-time data processing and connect to the internet in a separate
thread. Here is how I release the first one:

               for (int i =0 ; i< analyseBitmaps.length ; i++)
               {
                           if ( analyseBitmaps[i] != null )
                           {
                                   if ( !analyseBitmaps[i].isRecycled())
                                           analyseBitmaps[i].recycle();
                           if (analyseBitmaps[i].isRecycled())
                                           System.out.println("Destroyed 
analyseBitmaps nr: "+i);
                   }
               }

I need to load them one-by-one because if I load both of them at the
beggining I have an OOME.

3. After the processing is over I finish this activity and launch a
2nd one to view the results. I have Overrided the onDestroy() method
to release the bitmaps:


          @Override
        public void onDestroy()
            {
                super.onDestroy();

               for (int i =0 ; i< analyseBitmaps.length ; i++) //first
animation
               {
                           if ( !analyseBitmaps[i].isRecycled())
                                   analyseBitmaps[i].recycle();
                           if (analyseBitmaps[i].isRecycled())
                                   System.out.println("Destroyed analyseBitmaps 
nr: "+i);
               }

                 for (int i =0 ; i< connectingBitmaps.length ; i++)//second
animation
               {
                           if ( !connectingBitmaps[i].isRecycled())
                                   connectingBitmaps[i].recycle();

                           if (connectingBitmaps[i].isRecycled())
                                           System.out.println("Destroyed 
connectingBitmaps nr: "+i);
               }

               animationImage.setBackgroundResource(0);

            }

4. The second activity show the results and has a button that gets
back to the first activity.

Now what is the problem. If I launch the app and keep it working
normally everything is doing fine. I can keep the activity going on
and on and on - no memory leaks. Until I have pressed the 'back'
button while the 1 animation was working, and came to the app again
and I had an OOME. What I figured out is that my OnDestroy method
would delete the analyseBitmaps (and only them) but the app keeps on
working, launches the 2nd animation and after finishing the work shows
the 2nd activity. So the connectingBitmaps are not released in
onDestroy (because they are not loaded yet) and they cause the leak. I
need to find a way to release connectingBitmaps after the 1st activity
stops working(despite being destroyed already) in the OS background. I
can't release them just at the end of the 1st activity before calling
finish() because I have this exception:
java.lang.RuntimeException: Canvas: trying to use a recycled bitmap
android.graphics.bit...@2fdccb38.

So my questions are:
1. How to cover this situation so that the 2nd animation would be
destroyed when the 'back' button is pressed?
2. I can imagine a situation when the user launches the app, and
accidentally presses the back button. Then he want to immediately
relaunch the app again. So 2 instance of the app would be working at
the same time. I tried that and it leaded to super-slow work and
eventual error. How to cover this?

This is sth. I can't quite comprehend and couldn't get any clue on
google, which seems odd to me because sb. must have run into similar
issues with 'back' button. Can any one explain this?

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to