Search code examples
androidperformanceandroid-layoutleakcanarytimber-android

May changing the visibility of too many layouts a performance issue?


Suppose you have a FrameLayout containing 10 LinearLayouts, where only one is visible per time.

Each LinearLayout is a complex view, containing Button, EditText, TextView, etc.

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/alice
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible">

        <!-- complex stuff -->

    </LinearLayout>

    <!-- many more linear layouts... -->

    <LinearLayout
        android:id="@+id/juliett
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone">

        <!-- last complex stuff -->

    </LinearLayout>

</FrameLayout>

Thus:

  1. Changing the LinearLayout visibility, in order to show another item, would be a huge performance issue?
  2. Given it is an issue, why using ViewFlipper does not slow down the app performance?

Solution

  • It's bad practice because the code easily become a mess. Ignoring that and focusing only on performance, when you set the visibility to GONE, the view isn't measured (it's different from INVISIBLE). The view occupies a little bit of memory, though. Depending on what you're doing, consider using ViewGroup.removeView().

    It's hard to say without a benchmark, but theoretically it shouldn't have performance issues.