I’ll use English in following blogs, because it’s easy to copy and paste. lol.
2018-11-13 15:31:21
codelab
An activity is started or activated with an intent. An Intent
is an asynchronous message that you can use in your activity to request an action from another activity, or from some other app component. You use an intent to start one activity from another activity, and to pass data between activities.
An Intent
can be explicit or implicit:
- An explicit intent is one in which you know the target of that intent. That is, you already know the fully qualified class name of that specific activity.
- An implicit intent is one in which you do not have the name of the target component, but you have a general action to perform.
In this blog, I’ll use explicit intent, implicit intent will be explained later.
What you will learn:
- How to define parent and child activities for Up navigation.
- How to start an
Activity
with an explicitIntent
. - How to pass data between each
Activity
with an explicitIntent
.
Create child activity
<activity android:name=".SecondActivity"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="moe.leer.twoactivities.MainActivity"/>
</activity>
With android:parentActivityName
attribute, We set the SecondActivity as the child of MainActivity. This relationship is used for Up navigation in your app: the app bar for the second activity will have a left-facing arrow so the user can navigate “upward” to the main activity.
The meta-data
tag is same as android:parentActivityName
.This tag is for android API levels 16 and below.Because the android:parentActivityName.
is only available for API levels higher than 16. If your minSdkVersion in build.gradle is greater then 16, it’ ok to delete meta-data
tag.
Send data to SecondActivity
In MainActivity:
fun launchSecondActivity(view: View) {
Log.d(TAG, "Button clicked!")
val intent = Intent(this, SecondActivity::class.java).apply {
putExtra(EXTRA_MESSAGE, editText.text.toString())
}
startActivity(intent)
}
In SecondActivity:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
val message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE)
textMessage.text = message
}
Return data back to MainActivity
We can use startActivityForResult
to let second activity return back data. The onActivityResult
method will be called when data is received.
In MainActivity:
fun launchSecondActivity(view: View) {
Log.d(TAG, "Button clicked!")
val intent = Intent(this, SecondActivity::class.java).apply {
putExtra(EXTRA_MESSAGE, editText.text.toString())
}
startActivityForResult(intent, TEXT_REQUEST)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == TEXT_REQUEST && resultCode == Activity.RESULT_OK) {
text_header_reply.visibility = View.VISIBLE
text_message_reply.text = data!!.getStringExtra(SecondActivity.EXTRA_REPLY)
text_message_reply.visibility = View.VISIBLE
} else {
Toast.makeText(this, "NO data reply", Toast.LENGTH_SHORT).show()
}
}
In SecondActivity:
fun returnReply(view: View) {
val replyIntent = Intent().apply {
putExtra(EXTRA_REPLY, editText2.text.toString())
}
setResult(Activity.RESULT_OK, replyIntent)
finish()
}
Summary
- An
Intent
lets you request an action from another component in your app, for example, to start oneActivity
from another. AnIntent
can be explicit or implicit. - With an explicit
Intent
you indicate the specific target component to receive the data. - With an implicit
Intent
you specify the functionality you want but not the target component. - An
Intent
can include data on which to perform an action (as a URI) or additional information asIntent
extras. Intent
extras are key/value pairs in aBundle
that are sent along with theIntent
.
Homework
Question 1
What changes are made when you add a second Activity
to your app by choosing File > New > Activity and an Activity
template? Choose one:
- The second
Activity
is added as a Java class, the XML layout file is created, and theAndroidManifest.xml
file is changed to declare a secondActivity
.
Question 2
What happens if you remove the android:parentActivityName
and the <meta-data>
elements from the second Activity
declaration in the AndroidManifest.xml
file? Choose one:
- The Up button in the app bar no longer appears in the second
Activity
to send the user back to the parentActivity
.
Question 3
Which constructor method do you use to create a new explicit Intent
? Choose one:
new Intent(Context context, Class<?> class)
Question 4
In the HelloToast app homework, how do you add the current value of the count to the Intent
? Choose one:
- As an
Intent
extra
Question 5
In the HelloToast app homework, how do you display the current count in the second “Hello” Activity
? Choose one:
- Get the
Intent
that theActivity
was launched with. - Get the current count value out of the
Intent
. - Update the
TextView
for the count. - All of the above.