https://kotlinlang.org logo
#compose
Title
# compose
l

lib

09/25/2020, 3:33 AM
How to route from one screen to another ?
n

nglauber

09/25/2020, 3:37 AM
AFAIK, there’s nothing specific for compose yet. But you can use regular navigation mechanisms (activities and fragments)
l

lib

09/25/2020, 4:27 AM
Thank you,but I need just one single Activity
s

SaurabhS

09/25/2020, 4:28 AM
There's a Compose Navigation library, but it's still only available in snapshot releases. There are third party options like: https://github.com/zsoltk/compose-router
l

lib

09/25/2020, 4:30 AM
I know this, but it’s not perfect.
🆗 1
🤣 1
a

Afzal Najam

09/25/2020, 1:37 PM
I've been using the NavigationViewModel from jet news sample. Seems to be okay, not the best but it works with a few modifications.
l

lib

09/25/2020, 2:55 PM
thank you
b

Brian Beale

09/26/2020, 2:31 PM
Mine does not restore after process death; that is a nice trick I'd like to work in from that NavigationViewModel
v

Val Salamakha

09/27/2020, 7:04 AM
@Brian Beale To avoid the error “@Composable invocations can only happen from the context of a @Composable function”, replace code as following:
Copy code
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val backStack = BackStack()
        onBackPressedDispatcher.addCallback(this, backStack.callback)

        val buttonPressed = mutableStateOf(false)

        setContent {
            MaterialTheme {
                // A surface container using the 'background' color from the theme
                Surface(color = MaterialTheme.colors.background) {
                    Providers(AmbientBackStack provides backStack) {
                        NavHost(AmbientBackStack.current) {
                            println("NavHost")
                            Button(onClick = {
                                buttonPressed.value = true
                                println("1. Button pressed ${buttonPressed.value} ")
                            }) {
                                Text("Navigate forward")
                            }
                            if (buttonPressed.value) {
                                Push(true)
                                buttonPressed.value = false
                                println("2. Button pressed ${buttonPressed.value}")

                            }
                        }
                    }
                }
            }
        }
    }
}

@Composable
fun Push(yes : Boolean){
    if (yes) {
        AmbientBackStack.current.push {
            Text("Use the back button to go back")
        }
    }
}
b

Brian Beale

09/27/2020, 3:28 PM
Thanks for catching that error; I made a mistake copying things over. The @Composable invocation error is caused by AmbientBackStack.current, so if you assign that to a variable and then use that variable in the callback instead, the error should be resolved without the extra boolean state.
v

Val Salamakha

09/27/2020, 11:19 PM
Thanks for pointing to this way.