International Islamic UniversityH-10, Islamabad, Pakistan
Mobile Applications Development
Week 06
Using Explicit Intents
in Android
Engr. Rashid Farid Chishti
http://youtube.com/rfchishti
http://sites.google.com/site/chisht
i
2.
ďŽ Understanding Intents
ďŽLearn to send and receive data via intents.
ďŽ Sending back data using Back Intent
Objective
3.
ďŽ In Android,an explicit intent is an intent that clearly specifies the target
component (such as a particular Activity, Service, or BroadcastReceiver) that
should handle the request.
ďŽ In simple words:
ďą With an explicit intent, you tell exactly which component you want to open.
ďą This is mostly used inside your own app, when you want to move from one
activity to another or start a known service.
ďŽ Use Case:
ďą Navigate from one screen (Activity) to another.
ďą Pass data between activities.
What are Explicit Intents?
4.
ďŽ Example ofExplicit Intent
ďą Suppose you have two activities: MainActivity and Activity_Second.
ďą To open Activity_Second from MainActivity, you can use an explicit intent:
ďą Here:
ďŽ this refers to the current context (MainActivity).
ďŽ Activity_Second::class.java tells Android exactly which activity to open.
ďŽ When to Use Explicit Intents
ďą Navigating between activities in the same app.
ďą Starting a service in your own app.
ďą Sending a broadcast to a specific receiver within your app.
Explicit Intents
5.
ďŽ You canpass data like strings, numbers, and even objects.
ďŽ Example
ďą // In FirstActivity
ďą // In Activity_Second
Passing Data with Intent Extras
6.
ďŽ Select App
ďŽClick File > New > Activity > Empty Views Activity
1. Practical Example
7.
ďŽ Activity Name
ďąActivity_Second
ďŽ Layout Name
ďą activity_second
ďŽ Press Finish Botton
1. Practical Example
val systemBars =insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right,
systemBars.bottom)
insets
}
// Retrieve data
val name = intent.getStringExtra("name")
val age = intent.getIntExtra("age", 0)
val tv_message = findViewById<TextView>(R.id.tv_message)
val btn_go_back = findViewById<TextView>(R.id.btn_go_back)
// Display Data
tv_message.text = "Welcome, $name! You are $age years old."
btn_go_back.setOnClickListener {
finish()
}
}
}
1. Explicit Intents (Activity_Second.kt) (1/2)
16.
ďŽ There isnâtan actual Intent called "Back", but developers often say âback
intentâ when they mean:
ďŽ Navigating back to the previous screen
ďą This usually happens when the user presses the Back button (system back
gesture or button).
ďą Android handles this automatically by finishing the current activity and
returning to the one below it in the stack.
2. Back Intent
val systemBars =insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right,
systemBars.bottom)
insets
}
val btn_go_back = findViewById<TextView>(R.id.btn_go_back)
btn_go_back.setOnClickListener {
val resultIntent = Intent().apply {
putExtra("RESULT_KEY", "Hello MainActivity!")
}
setResult(RESULT_OK, resultIntent) // attach result
finish() // close SecondActivity and return
}
}
}
2. Back Intent (Activity_Second.kt) (2/2)
23.
ďŽ Use explicitintents for internal communication.
ďŽ Always check for null values when retrieving data.
ďŽ Use constants for keys (const val KEY_NAME = "username") instead of
hardcoding.
ďŽ Use Parcelable/Serializable when passing complex objects.
Best Practices