Building multi-screen apps: intents and their applications, types of intents; data exchange from one activity to another using intent

एक intent एक activity शुरू करने के लिए Android का message है: explicit intents खोलने के लिए एक specific screen नाम देते हैं, और आप इसे एक activity से दूसरी तक carry करने के लिए putExtra से data attach करते हैं।

11 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

एक Screen कभी काफ़ी नहीं होती

FestConnect Mobile की event LIST एक screen है। एक event tap करना इसकी DETAIL screen खोलनी चाहिए, उस event की info दिखाते हुए। दो screens, और app को इनके बीच MOVE करना पड़ता है AND tapped event का data साथ carry करना पड़ता है।

Android में, हर screen एक activity है, और आप activities के बीच एक intent इस्तेमाल करके move करते हैं: एक message जो कहता है 'यह screen start करो'। आप BCA305-02 में Java से intents से मिले; यहाँ आप इन्हें Kotlin में इस्तेमाल करते हैं। यह lesson intents से multi-screen navigation, intent के दो types, और एक activity से दूसरी तक data कैसे pass करें cover करता है।

Theory

Activities और Intents

एक activity एक Android app में एक single screen है। एक intent एक message object है जो एक operation describe करता है, सबसे common 'एक activity start करो'।

Intent के TWO types हैं:

  • explicit intent: एक SPECIFIC target component नाम देता है (आपके OWN app में एक particular activity)। आपके app के अंदर navigation के लिए इस्तेमाल होता है: Intent(this, DetailActivity::class.java)
  • implicit intent: एक app नाम दिए बिना एक ACTION describe करता है (जैसे 'यह URL view करो'), SYSTEM को इसे handle करने वाला एक suitable app ढूँढने देते हुए। OTHER apps invoke करने के लिए इस्तेमाल होता है (अगला lesson)

Actually screen खोलने के लिए, startActivity(intent) call कीजिए। यह lesson EXPLICIT intents पर focus करता है, आपकी अपनी screens के बीच move करना, FestConnect Mobile जैसे एक multi-screen app के लिए everyday case।

Practical

Explicit Intent: List से Detail तक, Data Pass करना

// In the LIST activity: open the DETAIL activity for a tapped event
fun openDetail(eventName: String, seats: Int) {
    val intent = Intent(this, DetailActivity::class.java)   // EXPLICIT target
    intent.putExtra("eventName", eventName)   // attach data (extras)
    intent.putExtra("seats", seats)
    startActivity(intent)                     // go to the detail screen
}

// In DetailActivity's onCreate: READ the passed data
class DetailActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val name  = intent.getStringExtra("eventName")   // read extra
        val seats = intent.getIntExtra("seats", 0)       // default 0
        // ... display name and seats
    }
}

Theory

Extras से Data Pass करना

एक activity से दूसरी तक data carry करने के लिए, इसे START करने से PEHLE intent में extras attach कीजिए:

intent.putExtra("eventName", eventName)

Receiving activity इन्हें अपने INTENT से पढ़ता है:

val name = intent.getStringExtra("eventName")

हर extra एक KEY-VALUE pair है (putExtra(key, value) / getStringExtra(key), getIntExtra(key, default) वग़ैरह, type match करते हुए)। तो list activity tapped event का name और seats intent में pack करता है, और detail activity इन्हें unpack करता है: data intent के through screen से screen तक flow करता है। यह अगली screen को information देने का standard Android तरीका है, और यह exactly वह है जिससे FestConnect Mobile का detail page जानता है कौन सा event दिखाना है।

Quiz

आप अपने खुद app में एक specific screen (DetailActivity) खोलना चाहते हैं और इसे event name pass करना चाहते हैं। कौन सा intent type और mechanism?

  1. एक action string वाला एक implicit intent
  2. DetailActivity नाम देने वाला एक EXPLICIT intent, data putExtra से attached और getStringExtra से पढ़ा गया
  3. किसी intent की ज़रूरत नहीं है; बस activity का method call कीजिए
  4. एक broadcast intent
Show the answer

DetailActivity नाम देने वाला एक EXPLICIT intent, data putExtra से attached और getStringExtra से पढ़ा गया

अपने OWN app में एक SPECIFIC activity खोलने के लिए, target नाम देने वाला एक EXPLICIT intent इस्तेमाल कीजिए (Intent(this, DetailActivity::class.java)), और startActivity से पहले putExtra से data attach करके pass कीजिए; receiving activity इन्हें getStringExtra/getIntExtra से पढ़ता है। Option A (implicit intent) OTHER apps को ACTION से invoke करने के लिए है (एक URL खोलने जैसा), आपकी अपनी named screen navigate करने के लिए नहीं। Option C ग़लत है: आप बस एक activity का method call करके इसे नहीं दिखा सकते; activities intents और Android system के through launch होती हैं। Option D (broadcast) system-wide messages के लिए एक अलग mechanism है, screen navigation नहीं। Explicit intent + extras standard within-app navigation-with-data pattern है।

Think first

Explicit या Implicit: दो Jobs

FestConnect Mobile को (a) अपनी खुद की DetailActivity खोलनी है, और (b) fest की website एक browser में खोलनी है। हर एक के लिए कौन सा intent type, और क्यों? फिर tap कीजिए।

Show the answer

(a) अपनी खुद की DetailActivity खोलना एक EXPLICIT intent इस्तेमाल करता है: आप exactly जानते हैं कौन सा component start करना है (DetailActivity), तो आप इसे directly नाम देते हैं। Explicit intents आपके app के अंदर navigation के लिए हैं। (b) fest WEBSITE एक browser में खोलना एक IMPLICIT intent इस्तेमाल करता है: आप एक specific browser app नाम नहीं देते; इसकी बजाय आप ACTION describe करते हैं ('यह URL view करो') और Android system को इसे handle करने वाला एक app ढूँढने देते हैं (जो भी browser user के पास है)। Implicit intents दूसरे apps को इनकी capability से invoke करने के लिए हैं, नाम से नहीं। Rule: जब आप exact target जानते हैं (आपकी अपनी screens) explicit, जब आप एक action के लिए system को एक app ढूँढने देना चाहते हैं (एक URL खोलना, share करना, एक photo लेना) implicit। अगला lesson implicit intents को detail में cover करता है (web URLs, sharing)।

Watch out

Intent Traps

Explicit बनाम Implicit Confusion: explicit एक component नाम देता है (आपकी screens); implicit एक action describe करता है (दूसरे apps)।

ग़लत Extra Type पढ़ना: एक String के लिए getStringExtra, एक Int के लिए getIntExtra; mismatches null/default return करते हैं।

startActivity भूलना: एक intent create करना कुछ नहीं करता जब तक आप इसे start न करें।

Unregistered Activity: activities manifest में declared होनी चाहिए, नहीं तो launching crash करता है।

Extras के through बड़ा/complex Data Pass करना: extras छोटे data को suit करते हैं; बड़े objects को दूसरे approaches चाहिए (एक shared store, re-fetch करने के लिए ids)।

Theory

अपनी Screens हो गईं; अब दूसरे Apps

FestConnect Mobile explicit intents से अपनी screens navigate कर सकता है और data साथ carry कर सकता है। अगला lesson आपके app के OUTSIDE पहुँचने के लिए IMPLICIT intents इस्तेमाल करता है: fest website को एक browser में खोलना, और एक event को WhatsApp या दूसरे apps में share करना। Implicit intents आपके app को पूरे device के साथ cooperate करने देते हैं, अच्छे Android apps का एक hallmark। फिर SQLite app को persistent storage देता है, और location और camera device powers add करते हैं।

Summary

Key takeaways

  • एक activity एक single screen है; एक intent एक activity start करने (या एक operation perform करने) का एक message है।
  • Explicit intent: एक specific target component नाम देता है (Intent(this, DetailActivity::class.java)): आपके app के अंदर navigation के लिए।
  • Implicit intent: एक action describe करता है, system को इसे handle करने वाला एक app ढूँढने देते हुए: दूसरे apps invoke करने के लिए (अगला lesson)।
  • startActivity(intent) screen launch करता है।
  • Extras से data pass कीजिए: start करने से पहले intent.putExtra("key", value); intent.getStringExtra("key") / getIntExtra(key, default) से पढ़िए।
  • Data intent के extras के through एक activity से दूसरी तक flow करता है।
  • Memory hook: explicit screen नाम देता है, implicit action describe करता है, putExtra/getExtra data carry करते हैं।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from JSON, Intent and Storing Android Application data using Database

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Building multi-screen apps: intents and their applications, types of intents; data exchange from one activity to another using intent · Advance Mobile Technology - I (Major-11-02) · Gri-Learn