This would be hard to explain so I'll try to make it as simple as it can be. Also don't know if it is in fact a multimedia issue - more like a life cycle thing.
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:
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:
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:
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.Bitmap@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?