Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
📋 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)
Intents के दो प्रकार

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
    }
}
MainActivityAndroid systemDetailActivitystartActivity(intent)create + onCreate()user ने Close tap कियाfinish()MainActivity फिर दिखी (onResume)
Screen खोलना और बंद करना

नई 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 से काम

आपकी appACTION_SEND textAndroidकौन handle करेगा?ChooserWhatsApp · Gmail · TelegramUser चुनता हैapp खुलती है
कौन सी apps installed हैं, यह जानने की ज़रूरत नहीं

तैयार 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)))
9:41▮▮ 85%Contact us⋮📞 CALL INSTITUTE💬 WHATSAPP✉ EMAIL US📍 OPEN IN MAPSShare app9:41▮▮ 85%Share via⋮🛍️WhatsApp💬🛍️Gmail✉🛍️Telegram✈🛍️Copy📋
सिर्फ़ implicit intents से बनी contact screen

जब कोई 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()

DetailActivityऊपर – दिख रहीMainActivitystopped, इंतज़ार में
हर startActivity screen push करता है; Back या finish() उसे pop करता है
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_DIAL use करें।
  • 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 दबाने जैसा।
← Android Tutorial (Kotlin)
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

अगला topicActivities के बीच Data भेजें आपकी प्रगति: 20 / 43 (47%) · Lesson 21 / 43 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें