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

4

46

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

Anyhow answered 1/12, 2020 at 9:40 Comment(5)
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() and composable() destinations, though I have not tried that yet.Cartogram
Using 'ContextAmbient.current' gives Intent but Context is needed. Can you please show that in an example?Anyhow
No, ContextAmbient provides a Context. Though it appears to be being renamed to AmbientContext, based on the source code.Cartogram
@CommonsWare, please refer to the imageAnyhow
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).Cartogram
P
99

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")
    }
}
Pskov answered 7/12, 2020 at 22:16 Comment(0)
A
10

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)
}
Allamerican answered 18/9, 2021 at 23:47 Comment(0)
H
5

Instead of using LocalContext.current to navigate to another Activity from a Composable you can pass the onClick callback to Activity/Fragment and navigate like before. Here is an 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")
    }
}
Hippomenes answered 31/8, 2022 at 8:47 Comment(0)
T
0

startNewActivity is a function pass context , any class and Bundle it can be null ,

.clickable { startNewActivity(context,DetailsActivity::class.java) },

   fun  startNewActivity(context: Context,activityClass: Class<*>, bundle: Bundle? = null){
                val intent = Intent(context, activityClass)
            bundle.let {    intent.putExtras(bundle!!) }
                context.startActivity(intent)
            }
Terni answered 11/3, 2024 at 11:21 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.