Android: out of memory error when infinite activities?
I create an instance from activity A to B, then in activity B I create another instance of activity A (I want to keep the previous A in history). I keep creating instance and I have a history stack from Activity A -> B -> A -> B -> A.....and after like 20 times, the system looks like need more memory and will kill the first activity A, then B (it calls onDestroy() from Log)...each at a time when I create each activity. I never have any memory issue in this scenario. But if I suddenly create an activity C which has lots of images that requires large memory, it will throw an Out of Memory Error: VM wont let us to allocate 70k bytes something like that.
So anyone kno开发者_JAVA百科w when will system kill an activity? Does it kill only 1 activity each time? How can I let the system give me more memory before I start activity C? Do I have a memory leak on this? Any help is appreciated.
Android does not kill activities unless memory is demanded. They simply remain in memory, paused. If you are switching from Activity A to B and back, Android is simply changing which is visible and onResume()d - they are not being killed (to read more on this search on Android Activity Stack). A memory error on activity C probably is entirely an issue within C. Depending on your situation, it may be easy to change the AndroidManifest just to test launching C directly... you're likely to get the same result. FYI.. call finish() to exit an Activity manually and free resources.
精彩评论