Sobes.tech
Junior

What types of Intents exist in Android?

sobes.tech AI

Answer from AI

There are two main types of Intents:

  • Explicit Intents: Specify a particular component (Activity, Service, BroadcastReceiver) to start.

    // Example of an explicit intent to start SpecificActivity
    val intent = Intent(this, SpecificActivity::class.java)
    startActivity(intent)
    
  • Implicit Intents: Declare a general action to be performed, which components can handle. The Android system then finds suitable components registered to handle this action (via Intent filters).

    // Example of an implicit intent to open a web page
    val webpage: Uri = Uri.parse("http://www.example.com")
    val intent = Intent(Intent.ACTION_VIEW, webpage)
    // Check if there is an Activity that can handle this intent
    if (intent.resolveActivity(packageManager) != null) {
        startActivity(intent)
    }
    

Intents can also contain additional data (Extras) in key-value pairs.