📋 Screens, Lists और Navigation · Lesson 20
Intents: Screens और Apps खोलें
Intent क्या है?
Intent एक message है जो कहता है "मुझे कुछ करना है"। Android इसे सही component तक पहुँचाता है — आपकी app की दूसरी screen, या dialer, browser, WhatsApp जैसी दूसरी app।
Explicit intent
- Exact class का नाम
- Intent(this, DetailActivity::class.java)
- अपनी screens खोलें
Implicit intent
- Action + data बताता है
- Intent(ACTION_VIEW, "https://…")
- Android app चुनता है (browser)
Explicit intent: अपनी screen खोलें
File → New → Activity → Empty Views Activity से DetailActivity नाम की दूसरी screen बनाएँ। Android Studio Kotlin file, layout और 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
}
}नई Activity declare करें
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: दूसरी app से काम
तैयार examples
Kotlin – 7 आम 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)))जब कोई app handle न कर सके
अगर कोई installed app वह action support न करे (जैसे कोई maps app नहीं), startActivity ActivityNotFoundException फेंकता है। इसे wrap करें:
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 और finish()
Kotlin – login के बाद back stack साफ़
// 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()आम गलतियाँ
- Activity manifest में नहीं →
ActivityNotFoundException: Unable to find explicit activity class। - Permission के बिना
ACTION_CALL→SecurityException।ACTION_DIALuse करें। - URL में
https://भूलना → कुछ नहीं खुलता। - Non-Activity context से
FLAG_ACTIVITY_NEW_TASKके बिनाstartActivity→ crash।
सारांश
- Explicit:
Intent(this, X::class.java)+ manifest entry। - Implicit: action + data (ACTION_VIEW, ACTION_DIAL, ACTION_SEND, ACTION_SENDTO)।
- Sharing के लिए chooser use करें और
ActivityNotFoundExceptionपकड़ें।
✅ Practice
पाँच buttons वाली "Contact the Institute" screen बनाइए: call, WhatsApp, email, maps और share — हर एक implicit intent से।
अक्सर पूछे जाने वाले प्रश्न (FAQ)
Explicit और implicit intents में क्या फ़र्क है?
Explicit intent खोलने वाली exact class बताता है (DetailActivity::class.java)। Implicit intent सिर्फ़ action बताता है (web page ACTION_VIEW) और Android ऐसी app ढूँढता है जो यह कर सके।
Number dial करने के लिए CALL_PHONE permission चाहिए?
नहीं। ACTION_DIAL बिना किसी permission के number भरा हुआ dialer खोलता है। ACTION_CALL सीधे call लगाता है और CALL_PHONE चाहिए।
finish() क्या करता है?
यह current Activity बंद करके back stack की पिछली पर लौटाता है, Back दबाने जैसा।
