I have the following two activities, ActivityA and ActivityB.
In ActivityA.java
startActivity(new Intent(this, ActivityB.class));
finish();
In ActivityB.java
public void onResume() {
super.onResume();
anim1 = new TranslateAnimation(0, 0, fromYDelta, -height);
anim1.setDuration(8000);
anim1.setFillAfter(true);
anim1.setStartOffset(0);
aView.startAnimation(anim1);
}
After the code in ActivityA that starts ActivityB and finishes itself has run, the activity life cycle callbacks were called in this order:
ActivityA.onPause();
ActivityB.onStart();
ActivityB.onResume(); //Start 8 seconds animation here
ActivityA.onStop();
ActivityA.onDestroy();
Now the issue is that ActivityA.onStop() was called 8 seconds after ActivityB.onResume() is called because of the 8-seconds animation started in ActivityB’s onResume(). It caused OutOfMemoryError in my app, because ActivityA.onDestroy() was called very late and the system had to hold all the resources in ActivityA for a long time.
Any suggestions? Thanks!
I was investigating this issue before and found out that Android OS does not guarantee that activity will be destroyed as soon as it goes to background. First of all, priority will be given to the new activity to get initialized and go to IDLE state. Only after that your previous activity will get destroyed.
So there is no native solution for that. You should not expect onDestroy() to be called as soon as you call
finish(). It is all up to OS when to call itCheck these threads – probably it will shed some light on your problem:
https://groups.google.com/forum/?fromgroups#!topic/android-developers/aDa7fiVL2Zg
https://groups.google.com/forum/?fromgroups#!topic/android-developers/jkj_PvE9O1A