I have 5 FrameLayout, In all screen i am using AnimationDrawable
class for some animation and when Activity goes to another activity or further.
Edit 1 for code snippet what I am Using :
iView_cow.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
try {
startCowBlinking.stop();
iView_cow.setBackgroundResource(R.drawable.cow_turn_movement);
startCowAnimation = (AnimationDrawable) iView_cow.getBackground();
startCowAnimation.start();
mHandler.postDelayed(r, 4000);
} catch (Exception e) {
// TODO: handle exception
}
}
});
mHandler = new Handler();
r = new Runnable() {
@Override
public void run() {
try {
if(startCowAnimation.isRunning()){
startCowAnimation.stop();
iView_cow.setBackgroundResource(R.drawable.cow_blink);
startCowBlinking = (AnimationDrawable) iView_cow.getBackground();
startCowBlinking.start();
}
} catch (Exception e) {
// TODO: handle exception
}
}
};
Like this animation, I am using 3-4 animation on each activity. Application run fine but After some time I got this error:
dalvikvm-heap: Out of memory on a 4840016-byte allocation.
And
10-26 00:57:58.601: E/AndroidRuntime(12952): FATAL EXCEPTION: main
10-26 00:57:58.601: E/AndroidRuntime(12952): java.lang.OutOfMemoryError
10-26 00:57:58.601: E/AndroidRuntime(12952): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
10-26 00:57:58.601: E/AndroidRuntime(12952): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:577)
10-26 00:57:58.601: E/AndroidRuntime(12952): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:445)
10-26 00:57:58.601: E/AndroidRuntime(12952): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:775)
10-26 00:57:58.601: E/AndroidRuntime(12952): at android.content.res.Resources.loadDrawable(Resources.java:1998)
10-26 00:57:58.601: E/AndroidRuntime(12952): at android.content.res.Resources.getDrawable(Resources.java:707)
10-26 00:57:58.601: E/AndroidRuntime(12952): at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:280)
10-26 00:57:58.601: E/AndroidRuntime(12952): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:869)
10-26 00:57:58.601: E/AndroidRuntime(12952): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:806)
10-26 00:57:58.601: E/AndroidRuntime(12952): at android.content.res.Resources.loadDrawable(Resources.java:1983)
10-26 00:57:58.601: E/AndroidRuntime(12952): at android.content.res.Resources.getDrawable(Resources.java:707)
For solution I did two things when Activity Switch
- Finish the currrent activity by finish();
- Finish all the AnimationDrawable by using stop();
It helped me to recover this error but not longer
. So someone suggest me to what shold I do ?