Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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")
       }
like image 634
Phantom Avatar asked Apr 04 '21 10:04

Phantom


People also ask

How do you start an intent?

To start an activity, use the method startActivity(intent) . This method is defined on the Context object which Activity extends. The following code demonstrates how you can start another activity via an intent. # Start the activity connect to the # specified class Intent i = new Intent(this, ActivityTwo.

How do you start a composable activity?

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. Using 'ContextAmbient. current' gives Intent but Context is needed.

What is intent in mobile application development?

An intent is to perform an action on the screen. It is mostly used to start activity, send broadcast receiver,start services and send message between two activities. There are two intents available in android as Implicit Intents and Explicit Intents. Here is a sample example to start new activity with old activity.


1 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")
}
like image 104
Gabriele Mariotti Avatar answered Oct 08 '22 00:10

Gabriele Mariotti