Thread
#compose
    bkenn

    bkenn

    2 years ago
    I would like to navigate to a splash screen, perform an animation for a specified duration and then navigate to another screen. Is there a better way than placing the following inside my splash screen composable function? The navigateTo modifies a property in a Model object.
    GlobalScope.launch(Dispatchers.Main) {
        // replace delay with some kind of animation on ui elements
        delay(2000)
        navigateTo(Screen.Question)
    }
    Adam Powell

    Adam Powell

    2 years ago
    You will have issues if you put that directly into a composable function rather than in an
    onCommit
    effect block or similar; if your UI gets recomposed you will get multiple launches happening
    ideally you should be able to
    .await
    the end of an animation or something like it; we don't have a lot of robust suspending APIs in the base compose libraries yet since the version of the IR compiler we're using still has some bugs that make working with suspend prohibitive. Those APIs will come once those bugs are resolved
    bkenn

    bkenn

    2 years ago
    Thank you for the input! I am reading up on the differences between
    onCommit
    and
    onActive
    . Would it not be better for this use case to use
    onActive
    ? I put some logging into both
    onActive
    and
    onCommit
    of the splash screen compostable function.
    OnActive
    was called once while
    onCommit
    was called 11 times. Seems that however my code is currently structured is causing the splash screen to recompose a lot.
    Adam Powell

    Adam Powell

    2 years ago
    onActive
    is probably appropriate in here, but it's also likely to be removed in a later update. Its docs describe that it's just an alias for essentially
    onCommit(true) { }
    or
    onCommit(Unit) { }
    or some other constant that will never change
    we're likely to make a pass on things like this where there are many ways of doing the same thing in layers that don't add much value; whether this adds enough value to stay is probably a matter for debate 🙂
    I'm particularly sensitive to anything that looks like alternative lifecycle events; folks are fatigued enough after activities, fragments, services, jobs, workers...