📋 Screens, Lists & Navigation · Lesson 20
Intents: Open Screens & Apps
What is an Intent?
An Intent is a message that says "I want to do something". Android delivers it to the right component — another screen of your app, or another app such as the dialer, browser or WhatsApp.
Explicit intent
- Names the exact class
- Intent(this, DetailActivity::class.java)
- Open your own screens
Implicit intent
- Describes an action + data
- Intent(ACTION_VIEW, "https://…")
- Android picks an app (browser)
Explicit intent: open your own screen
Create a second screen with File → New → Activity → Empty Views Activity named DetailActivity. Android Studio creates the Kotlin file, the layout and the manifest entry.
Kotlin
// MainActivity.kt – open the detail screen
binding.btnOpen.setOnClickListener {
val intent = Intent(this, DetailActivity::class.java)
startActivity(intent)
}
// DetailActivity.kt
class DetailActivity : AppCompatActivity() {
private lateinit var binding: ActivityDetailBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityDetailBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.btnClose.setOnClickListener { finish() } // back to MainActivity
}
}Declare the new Activity
XML – AndroidManifest.xml
<!-- AndroidManifest.xml (Android Studio adds this when you use New → Activity) -->
<application>
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DetailActivity"
android:exported="false"
android:parentActivityName=".MainActivity" />
</application>Implicit intents: let another app do it
Ready-made examples
Kotlin – 7 common intents
// 1. Open a website
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://codingeasily.in")))
// 2. Dial a phone number (no permission needed)
startActivity(Intent(Intent.ACTION_DIAL, Uri.parse("tel:+919000000000")))
// 3. Share text (WhatsApp, Telegram, Gmail … user chooses)
val send = Intent(Intent.ACTION_SEND).apply {
type = "text/plain"
putExtra(Intent.EXTRA_TEXT, "Learn Android free: https://codingeasily.in/tutorial/android/")
}
startActivity(Intent.createChooser(send, "Share via"))
// 4. Send an email
val email = Intent(Intent.ACTION_SENDTO).apply {
data = Uri.parse("mailto:")
putExtra(Intent.EXTRA_EMAIL, arrayOf("help@codingeasily.in"))
putExtra(Intent.EXTRA_SUBJECT, "Doubt in lesson 20")
}
startActivity(email)
// 5. Open a place in Google Maps
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=Aligarh Muslim University")))
// 6. WhatsApp chat with a message
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://wa.me/919000000000?text=Hi")))
// 7. App settings page (e.g. after a permission was denied)
startActivity(Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.fromParts("package", packageName, null)))When no app can handle it
If no installed app supports the action (e.g. no maps app), startActivity throws ActivityNotFoundException. Wrap it:
Kotlin – extension function
fun Context.openSafely(intent: Intent) {
try {
startActivity(intent)
} catch (e: ActivityNotFoundException) {
Toast.makeText(this, "No app found to open this", Toast.LENGTH_SHORT).show()
}
}
// usage
openSafely(Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=Taj Mahal")))Back stack and finish()
Kotlin – clear the back stack after login
// After login: open Home and remove Login from the back stack
val intent = Intent(this, HomeActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
startActivity(intent)
// or simply: startActivity(...); finish()Common mistakes
- Activity not in the manifest →
ActivityNotFoundException: Unable to find explicit activity class. - Using
ACTION_CALLwithout permission →SecurityException. UseACTION_DIAL. - Forgetting
https://in a URL → nothing opens. - Calling
startActivityfrom a non-Activity context withoutFLAG_ACTIVITY_NEW_TASK→ crash.
Summary
- Explicit:
Intent(this, X::class.java)+ manifest entry. - Implicit: action + data (ACTION_VIEW, ACTION_DIAL, ACTION_SEND, ACTION_SENDTO).
- Use a chooser for sharing and catch
ActivityNotFoundException.
✅ Practice
Make a "Contact the Institute" screen with five buttons: call, WhatsApp, email, maps and share — each one using an implicit intent.
Frequently Asked Questions
What is the difference between explicit and implicit intents?
An explicit intent names the exact class to open (DetailActivity::class.java). An implicit intent only describes an action (ACTION_VIEW a web page) and Android finds an app that can do it.
Do I need the CALL_PHONE permission to dial a number?
No. ACTION_DIAL opens the dialer with the number filled in, without any permission. ACTION_CALL places the call directly and needs CALL_PHONE.
What does finish() do?
It closes the current Activity and returns to the previous one in the back stack, like pressing Back.
