_

    _shtomar

    10 months ago
    Is there any callback or listener to get an event when Composable rendered it’s content for first time or it’s content is changed?
    n

    nglauber

    10 months ago
    you can achieve something similar using this:
    @Composable
    fun Foo() {
        // ...
        LaunchedEffect(Unit) {
            // This code will run just on
            // the first composition
        }
    }
    However, if you remove
    Foo
    from the composition, it will be called again… For instance:
    if (someFlag) {
        Foo()
    } else {
        Bar()
    }
    every time you set
    someFlag
    to true, the code inside the
    LaunchedEffect
    will be executed.
    Adam Powell

    Adam Powell

    10 months ago
    What are you looking to use that callback event to do? Like @nglauber mentions above you can use the effect APIs to publish data about a recomposition, but there are many ways content can change and we try very hard to make sure that as many of them as possible have fast paths that skip running as much code as possible, and depending on what you want this for you might find yourself scratching your head as to why you aren't getting a callback you expect.
    _

    _shtomar

    10 months ago
    Thank @nglauber
    I am changing content of the Composable. Basically I need a callback whenver the content of the Composable changes
    K Merle

    K Merle

    10 months ago
    I think in that case you can replace Unit with the value of the content. So if you want to call LaunchedEffect when title changes only, you could do
    @Composable
    fun Foo() {
        // ...
        LaunchedEffect(title) {
            // This code will run just on
            // the first composition
        }
    }