Android: Intents (Kotlin)

There are two types of intents: Explicit and Implicit. The following examples and info come from this codelab.

Explicit intents are generally used to start another activity within your app. You can construct it with the constructor that takes the Context and the Activity class as parameters. Use Intent.putExtra to send parameters to the new activity. For example:

val intent = Intent(context, DetailActivity::class.java)
intent.putExtra(DetailActivity.LETTER, item.toString())
context.startActivity(intent)

Implicit intents are generally used to start an activity of the user’s choosing. For example you may have several browsers on your device that could be used to open a URL. You can construct it with the constructor that takes the Intent type and the Uri. For example:

val query = Uri.parse(DetailActivity.SEARCH_PREFIX + item)
val intent = Intent(Intent.ACTION_VIEW, query)
context.startActivity(intent)

Leave a Reply

Your email address will not be published. Required fields are marked *