First, usually you should call removeAllViews() instead of removeAllViewsInLayout() to read the JavaDoc for the difference.
In addition, none of these functions is recursive; they only remove representations from the object on which it is called. Substructures will not change. This can cause problems and even memory leaks if they have references to other objects that are stored.
For example, if you have the following structure:
<LinearLayout id='top'> <LinearLayout id='sub'> <Button id='b'/> </LinearLayout> </LinearLayout>
and call removeAllViews() on top sub layout will still refer to b (and b will refer to sub as the parent). This means that you cannot reuse b anywhere else without first deleting its parent ( sub ). If you do, you will get an exception.
source share