Android, do you know how many layers will Crash after the interface layout is nested? androidcrash
Let's first put a diagram of Hierarchy Viewer: (simulator Android4.4)
Do you see the number 6? The RelativeLayout is the root ViewGroup of MainActivity, and there are 5 layers before RelativeLayout, that is, the default system interface DecorView (FrameLayout) --> LinearLayout --> FramelLayout --> ActionBarOverlayLayout --> ContentFrameLayout.
Let's talk about the result directly. The simulator can be used to nest a maximum of 39 layers on API19, that is, Android4.4, including the default layer 5 of the system. That is to say, we can write a maximum of 34 layers of nesting.
When the number of nested layers reaches 40, the following exception is reported,
10-27 11:38:53. 901 25582-25582/com. aquarius. customview E/AndroidRuntime: fatal exception: main Process: com. aquarius. customview, PID: 25582 java. lang. stackOverflowError at android. util. pools $ SynchronizedPool. acquire (Pools. java: 154) at android. view. GLES20RecordingCanvas. obtain (GLES20RecordingCanvas. java: 43) at android. view. GLES20DisplayList. start (GLES20DisplayList. java: 61) at android. view. view. getDisplayList (View. java: 13323) at android. view. view. getDisplayList (View. java: 13404) at android. view. view. draw (View. java: 14182) at android. view. viewGroup. drawChild (ViewGroup. java: 3103) at android. view. viewGroup. dispatchDraw (ViewGroup. java: 2940) at android. view. view. getDisplayList (View. java: 13357) at android. view. view. getDisplayList (View. java: 13404) at android. view. view. draw (View. java: 14182) at android. view. viewGroup. drawChild (ViewGroup. java: 3103) at android. view. viewGroup. dispatchDraw (ViewGroup. java: 2940) at android. view. view. getDisplayList (View. java: 13357) at android. view. view. getDisplayList (View. java: 13404) at android. view. view. draw (View. java: 14182) at android. view. viewGroup. drawChild (ViewGroup. java: 3103) at android. view. viewGroup. dispatchDraw (ViewGroup. java: 2940) at android. view. view. getDisplayList (View. java: 13357) at android. view. view. getDisplayList (View. java: 13404) at android. view. view. draw (View. java: 14182) at android. view. viewGroup. drawChild (ViewGroup. java: 3103) at android. view. viewGroup. dispatchDraw (ViewGroup. java: 2940) at android. view. view. getDisplayList (View. java: 13357) at android. view. view. getDisplayList (View. java: 13404) at android. view. view. draw (View. java: 14182) at android. view. viewGroup. drawChild (ViewGroup. java: 3103) at android. view. viewGroup. dispatchDraw (ViewGroup. java: 2940) at android. view. view. getDisplayList (View. java: 13357) at android. view. view. getDisplayList (View. java: 13404) at android. view. view. draw (View. java: 14182) at android. view. viewGroup. drawChild (ViewGroup. java: 3103) at android. view. viewGroup. dispatchDraw (ViewGroup. java: 2940) at android. view. view. getDisplayList (View. java: 13357) at android. view. view. getDisplayList (View. java: 13404) at android. view. view. draw (View. java: 14182) at android. view. viewGroup. drawChild (ViewGroup. java: 3103) at android. view. viewGroup. dispatchDraw (ViewGroup. java: 2940) at android. view. view. getDisplayList (View. java: 13357) at android. view. view. getDisplayList (View. java: 13404) at android. view. view. draw (View. java: 14182) at android. view. viewGroup. drawChild (ViewGroup. java: 3103) at android. view. viewGroup. dispatchDraw (ViewGroup. java: 2940) at android. view. view. getDisplayList (View. java: 13357) at android. view. view. getDisplayList (View. java: 13404) at android. view. view. draw (View. java: 14182) at android. view. viewGroup. drawChild (ViewGroup. java: 3103) at android. view. viewGroup. dispatchDraw (ViewGroup. java: 2940) at android. view. view. getDisplayList (View. java: 13357) at android. view. view. getDisplayList (View. java: 13404) at android. view. view. draw (View. java: 14182) at android. view. viewGroup. drawChild (ViewGroup. java: 3103) at android. view. viewGroup. dispatchDraw (ViewGroup. java: 2940) at android. view. view. getDisplayList (View. java: 13357) at android. view. view. getDisplayList (View. java: 13404) at android. view. view. draw (View. java: 14182) at android. view. viewGroup. drawChild (ViewGroup. java: 3103) at android. view. viewGroup. dispatchDraw (ViewGroup. java: 2940) at android. view. view. getDisplayList (View. java: 13357) at android. view. view. getDisplayList (View. java: 13404) at android. view. view. draw (View. java: 14182) at android. view. viewGroup. drawChild (ViewGroup. java: 3103) at android. view. viewGroup. dispatchDraw (ViewGroup. java: 2940) at android. view. view. getDisplayList (View. java: 13357) at android. view. view. getDisplayList (View. java: 13404) at android. view. view. draw (View. java: 14182) at android. view. viewGroup. drawChild (ViewGroup. java: 3103) at android. view. viewGroup. dispatchDraw (ViewGroup. java: 2940) at android. view. view. getDisplayList (View. java: 13357) at android. view. view. getDisplayList (View. java: 13404) at android. view. view. draw (View. java: 14182) at android. view. viewGroup. drawChild (ViewGroup. java: 3103) at android. view. viewView Code
More layers can be nested on API23 Android6.0. crash is still unavailable after more than 40 layers of experiments.
I have not tested the specific results of versions earlier than 4.4, and Android open-source mobile phone manufacturers may modify the logic. However, considering the backward compatibility issue, we should not nest too many layers,
When you write more than 10 layers of layout layers, you should consider optimizing the layout, because even if the application does not crash due to too many nesting layers, rendering time is too long, the UI smoothness is affected.