Does Modifier.drawBehind run whenever any part of ...
# compose
l
Does Modifier.drawBehind run whenever any part of the canvas changes? I put a log statement both in the parent Composable and in the Modifier.drawBehind lambda. The parent is clickable, and whenever I click on it, I see that the drawBehind lambda gets called in the logs, but the parent does not. No state captured in drawBehind changed in this case.
example
Copy code
@Composable
fun Foo(width: Int, height: Int) {
    println("Composing Foo")
    val paint = remember { Paint() }
    val bitmap = remember(width, height) {
        createBitmap(IntSize(width, height))
    }
    Box(modifier = Modifier.fillMaxSize()
        .then(
            if(bitmap != null)
                Modifier.drawBehind {
                    println("drawBehind")     
                    drawIntoCanvas { canvas ->
                       ...
                    }
                }
            else Modifier
        )
    )
}

@Composable
fun ClickableFoo(width: Int, height: Int) {
    Box(modifier = Modifier.clickable(...)) {
        Foo(width, height)
    }
}
The paint and bitmap are never changed, so I wouldn’t expect it to need to run again.
c
Isn't there a
drawBehindCached
or something?
er...
drawWithCache
? ive never really used the draw apis. so im just regurgitating stuff that ive seen thrown around in this slack. 🙈
l
I'll have to try out drawWithCache tomorrow. It looks like what I need.
l
drawBehind
,
drawWithContent
,
drawWithCache
won't save their content. Add a
.graphicsLayer()
before
drawXxx
to enable caching.
c
this video was uploaded 8 days ago. it might help too.

https://www.youtube.com/watch?v=1yiuxWK74vI