How do I start an Intent inside a Compose function's button click

Viewed 4912

I want to redirect the user to another activity that opens a internet url. On button click.

Here is my code so far

Button(onClick = {
          val intent = Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"))
          // Here is some code that starts the activity
       }) {
          Text(text="APPLY HACK")
       }
2 Answers

You can use something like:

val intent = Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"))
val context = LocalContext.current

Button(onClick = {
    startActivity(context, intent, null) }
) {
    Text("BUTTON")
}
val context = LocalContext.current

Button(onClick = {
context.startActivity(Intent(Intent.ACTION_VIEW,Uri.parse("http://www.google.com"))) }
) {
    Text("Click here")
}
Related