I've 2 layouts on my project. Both have a button to swap between each other using setContentView method. Whenever I swap between these 2 layouts, every UI element I've added using addView() is lost. However static XML elements remains.
That's because the layout is inflated anew, with all the views specified in the xml when you call setContentView(R.layout.xml), that's happening behind the scenes, and all the dynamically added views will be gone.
Optional solutions:
GONE
to hide the layout, not INVISIBLE
If the layouts have clickable elements on them.