androidandroid-jetpackandroid-jetpack-navigationandroid-jetpack-compose

How to navigate from a Composable to an Activity in Jetpack Compose?


What are the ways in which navigation is possible between a composable and an Activity and vice versa? Can I do it by using the startActivity(...) method or is the only way to create Screens and NavController?

Android Studio Screenshot


Solution

  • In newer version of compose use LocalContext.
    In older versions (1.0.0-alpha08 and before) use AmbientContext:

    @Composable
    fun MainScreen() {
        val context = LocalContext.current
    
        Button(onClick = {
            context.startActivity(Intent(context, ListActivity::class.java))
        }) {
            Text(text = "Show List")
        }
    }