36

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

enter image description here

Sweta Jain
  • 3,248
  • 6
  • 30
  • 50
  • To have a composable start an activity, you can use `ContextAmbient` to get a `Context`. You might be able to use Navigation for Compose to create a nav graph that uses both [`activity()`](https://developer.android.com/guide/navigation/navigation-kotlin-dsl#activity) and [`composable()`](https://developer.android.com/jetpack/compose/navigation#create-navhost) destinations, though I have not tried that yet. – CommonsWare Dec 01 '20 at 12:10
  • Using 'ContextAmbient.current' gives Intent but Context is needed. Can you please show that in an example? – Sweta Jain Dec 02 '20 at 06:35
  • No, [`ContextAmbient` provides a `Context`](https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/package-summary#ContextAmbient:androidx.compose.runtime.ProvidableAmbient). Though it appears to be being renamed to `AmbientContext`, based on [the source code](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-master-dev:compose/ui/ui/src/androidMain/kotlin/androidx/compose/ui/platform/AndroidAmbients.kt;l=63-79?q=ContextAmbient&ss=androidx). – CommonsWare Dec 02 '20 at 11:49
  • 1
    @CommonsWare, please refer to the image – Sweta Jain Dec 03 '20 at 11:25
  • 1
    I do not know what `startActivity()` function you are trying to call. The error is showing that you are passing an `Intent` to it, and it is expecting a `Context` (at least as the first parameter). Your `Intent` is coming from the `Intent()` constructor that you are calling. I was expecting you to do `val context = ContextAmbient.current; context.startActivity(Intent(context, ListActivity::class.java))` (semicolon used here just because Stack Overflow comments can't handle newlines). – CommonsWare Dec 03 '20 at 13:52

3 Answers3

73

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")
    }
}
Aystub
  • 1,244
  • 11
  • 9
8

Here's how I usually do it (and pass values to another activity):

val context = LocalContext.current
...
onClick = {
    val intent = Intent(context, ListActivity::class.java)
    intent.putExtra(YourExtraKey, YourExtraValue)
    context.startActivity(intent)
}
2

Beside using LocalContext.current to navigate to another Activity inside Composable.

If you able to pass the onClick callback to Activity/Fragment, you still able to navigate like before. Example

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            AndroidTheme {
                Surface(...) {
                    Greeting (onClick = {
                        // this here is MainActivity

                        startDetailActivity()
                    })
                }
            }
        }
    }

    fun startDetailActivity() {
        val intent = Intent(this, DetailActivity::class.java)
        startActivity(intent)
    }
}

@Composable
fun Greeting(onClick: () -> Unit) {
    
    Button(onClick = onClick) {
        Text(text = "Button")
    }
}
Linh
  • 57,942
  • 23
  • 262
  • 279