Question

I am optimizing my android layouts. One suggestion I got was to re-use layouts in include statements. Although I understand the utility of this, kind of like object orientated views, but does it dramatically improve rendering time? Does it dramatically reduce memory footprint? Does it keep the view hierarchy smaller?

I wrote this assuming there was a definite answer to this, instead of a discussion

thanks for any insight!

Was it helpful?

Solution

Does it dramatically reduce memory footprint?

No. Here's a view that includes two views with the same content. While the views are the same, each item in the view must be presented as a separate instance since a developer might want to select it using R.id.identifier and change its content. Practically, lets say you include a header for your app in 2 different views. The header's contents need not be the same in each view. Since the instances are different there is no savings in memory.

screenshot

does it dramatically improve rendering time?

Not that I've noticed. Any gain you get here is likely to be minimal.

Here is a related link on performance optimization on Android in case you've not seen it already -> http://developer.android.com/training/articles/perf-tips.html

OTHER TIPS

Using include alone is probably not enough to yield any noticeable change to rendering time. It also shouldn't have an effect on the view hierarchy: you should get the same hierarchy regardless of whether portions of it are included from a separately-inflated layout.

(I suppose the LayoutInflater could be caching recently-inflated resources, potentially making it faster to inflate a layout that is composed of recently-used included pieces. But it seems unlikely, and unless you have benchmarked it in your app, this would be a needless micro-optimization IMO.)

What include is good for is organization of your layouts, in particular, breaking out reusable pieces. Romain Guy, author of much of the Android layout system, wrote a blog post a few years back showing some tips/tricks that are possible with include, for example using it to separate orientation-sensitive layouts from more generic ones.

By contrast, ViewStub is intended for layout inflate time optimization; it allows portions of your View hierarchy to be lazily inflated.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top