Home > Software design >  Type mismatch. Required: Context Found: Intent
Type mismatch. Required: Context Found: Intent

Time:10-06

I`m new to Compose and was trying to make my Text function go to a website, but get such an error. What is the problem?

pic of error

CodePudding user response:

try like this with context :

val intent = Intent(Intent.ACTION_VIEW, URI.parse("http://www.example.com"))
context.startActivity(intent)

CodePudding user response:

You should definitely read the Documentation.

Anyway, when you type method(), just move the cursor inside the parenthesis and press Ctrl P to get to know all the possible method signatures.

For the time being, this is how you are supposed to do this

@Composable
fun WebViewer(){
 val context = LocalContext.current
 Text(value = "Hello", onClick = { startActivity(context, Intent(/*Your Data Here*/)) })
}

CodePudding user response:

To use context inside a composable , you can use LocalContext.current

val intent = Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"))
LocalContext.current.startActivity(intent)
  • Related