I am looking to create a common header for an android activity and have 3 possible ways to solve the same. I would like to know which is the best solution in terms of performance and memory usage.
-
Copy the XML across all XML Layouts : I believe this is the least impressive solution which uses a lot of memory
-
Create a custom HeaderView , which I can then use where needed. Drawbacks : it still creates a lot of instances.
-
Use
<include >to include an XML component , not sure how it helps performance and reduces memory usage.
So which method is the best , or is there a better way?
I think you are worrying about the wrong thing. Pick the one that’s best for your project with regards to development or maintainability. Remember that Views are all part of ONE activity which would be visible to the user at any time, per the Android activity lifecycle. Its the framework’s responsibility to clean up non-visible activities if there is a need to garbage collect. If you are concerned about View performance, look into using Hierarchy Viewer which comes with the SDK to flatten your views.