Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are "GONE" views detrimental to performance?

I'm making an app in which it might save me some time to have a single layout for several activities, with some of the views set to GONE depending on which activity is being used.

I know that having a large number of views in a layout can lead to poor performance. If I had an activity with a large number of views, but a large portion of those views were to to GONE, would this activity still perform poorly. That is, do views that are set to GONE contribute to worsening performance? If yes, do they demand less processing power than VISIBLE or INVISIBLE views?

Thanks!

like image 998
public static void Avatar asked Jul 26 '13 14:07

public static void


2 Answers

First thing you should know about gone vs invisible:

  • View.GONE This view is invisible, and it doesn't take any space for layout purposes.
  • View.INVISIBLE This view is invisible, but it still takes up space for layout purposes.

Thinking about the impact on measuring. Which one is more efficient all depends on how frequently you are changing the view's visibility.

For example, if the view is not visible for a majority of the time, making it GONE would probably be more efficient, because the system would not be needlessly measuring and laying out your invisible view whenever it needs to adjust other views on the screen.

On the other hand, if the view changes between visible and invisible frequently, you might get better performance from INVISIBLE as you would potentially avoid an extra measure/layout on each transition.

like image 138
MSA Avatar answered Nov 14 '22 23:11

MSA


Here is an interesting answer. I was wondering the same thing as you, and the answer is that View.GONE consumes more memory than simply calling removeView(view) on the view. However, GONE views do consume less memory than View.VISIBLE since they do not need to be drawn.

The memory amounts compare like this:

View.VISIBLE > View.GONE > removing the view from the container

What I do is use View.GONE on views that don't consume a lot of memory (like a TextView) and use parent.removeView(view) on views that are a lot of memory (like a WebView);

like image 44
anthonycr Avatar answered Nov 15 '22 01:11

anthonycr