So your problem seems to be that "when you go on adding too many activity to the stack app throws OOM exception gradually", and you think that the cause could be that onDestroyView()
is not called on the top Fragment, when switching activities.
OnDestroyView()
First, when you go from Activity1 to Activity2, most likely onDestroyView()
is not called on your Fragment in Activity1 because you didn't call finish()
in Activity1, after starting Activity2.
This means that your Activity1 is alive and well in the activity backstack, but stopped (i.e. onStop called). Since Activity1 is alive, so is its backstack & fragments. The fragment at the top of the backstack of Activity1 will also be just stopped. So basically, Activity1 is in a state similar to the one it enters when you send your app to the background using the Home button.
Among other cases, onDestroyView()
is called on a Fragment when another fragment is added above it in the fragment backstack. However, it has no knowledge about the activity backstack/activity tasks whatsoever.
If you want to clear the view of your fragment, you can do it manually (i.e. fragmentManager.popbackstack()
, or beginTrasaction.remove(...)
), or you can close Activity1 after starting Activity2(i.e. call finish()
) - this will also free your memory and call onDestroyView()
on the top fragment in Activity1.
OutOfMemoryException
...when I go on adding too many activity to the stack app throws OOM
exception gradually.
Most likely the source of your OOM crash is that you have too many Activity instances in your memory and not that onDestroyView()
is not called on certain fragments. I also assume you have multiple instances of the same Activity.
Please consider using android:launchMode="singleTask"
(reference) when declaring your Activities in AndroidManifest.xml
. This ensures that you'll only have a single instance of a certain Activity within your given task. This, by itself should fix your OutOfMemory issue if it was caused strictly by too many instances of the same Activity.
This approach by itself will imply certain additional handling from your part, to reset a reused activity's UI/state back to a "clean" one. Luckily, you can rely on onNewIntent(...)
(reference) to detect when you need to do that.
Later edit: Memory Monitor
Regarding searching for the cause of your OutOfMemory error: Please use the Android Memory Monitor to search for memory leaks. I found memory leaks to be pesky little devils and using the Memory Monitor from the start is always better when compared to (informed) guessing.
In your case, after using your app for a while, and after performing a few activity switches, you need to see whether you have several instances of a particular activity (Activity1 for instance), in memory.
Basically, you need to look for something similar this:
Just remember to force the garbage collector a few times(just once is not enough) before taking the heap dump. This is to make sure that references which would be garbage collected at some point won't appear in your dump.
Hope this helps