eneim
05/05/2025, 12:20 PMremoveAllViews()
when childCount
is <= 0
in the else
flow. In stead, should the removeAllViews()
is called right before creating a new AndroidComposeView
instance and in the same block:
// Suggestion:
val composeView = if (childCount > 0) { getChildAt(0) as? AndroidComposeView } else { null } ?: /* at this point we know the parent doesn't contain any AndroidComposeView, regardless of its childCount, so we have to ensure it is empty and then add a new AndroidComposeView to it */ run {
removeAllViews()
// Create a new AndroidComposeView and "also" add it to "this".
}
From the code, it seems that, if childCount > 0
but the first child is not an AndroidComposeView
, the parent doesn't clear it before adding the newly created one.
Also, (AFAIK) even when childCount <= 0
, calling removeAllViews()
will cause a layout traversal so doing it seems to be unnecessary.shikasd
05/05/2025, 2:53 PMremoveAllViews
is usually followed by addView
which causes relayout anyways
It is added here in case someone tries to add a different type of view to the container (see as?
, it might return null)eneim
05/06/2025, 12:23 AMIt is added here in case someone tries to add a different type of view to the containerShould it be added where
childCount
is not zero? If I understand it correctly, the current code does removeAllViews()
when the parent is empty, which doesn't do anything meaningful.shikasd
05/06/2025, 11:30 AMeneim
05/06/2025, 11:37 AMchildCount > 0
and the first child is not an AndroidComposeView
, the if/else block returns null
(and so a new AndroidComposeView
will be created and added) without any removeAllViews()
call, which I think is a more critical situation.shikasd
05/06/2025, 11:38 AMshikasd
05/06/2025, 11:39 AMshikasd
05/06/2025, 11:39 AMshikasd
05/06/2025, 11:39 AMeneim
05/06/2025, 11:41 AMshikasd
05/06/2025, 11:43 AMeneim
05/06/2025, 11:48 AM