If your layout didn't turn out as expected, click See the final layout XML below to see what your XML should look like. Compare it to what you see in the Code tab. If your attributes appear in a different order, that's okay.
Expect Android Studio to encounter Cannot resolve symbol errors again. To clear the errors, press Alt+Enter, or Option+Return on a Mac. Your should end up with the following imports:
An error still remains for DisplayMessageActivity
, but that's okay. You fix it in the next section.
Here's what's going on in sendMessage()
:
The
Intent
constructor takes two parameters, aContext
and aClass
.The
Context
parameter is used first because theActivity
class is a subclass ofContext
.The
Class
parameter of the app component, to which the system delivers theIntent,
is, in this case, the activity to start.The
putExtra()
method adds the value ofEditText
to the intent. AnIntent
can carry data types as key-value pairs called extras.Your key is a public constant
EXTRA_MESSAGE
because the next activity uses the key to retrieve the text value. It's a good practice to define keys for intent extras with your app's package name as a prefix. This ensures that the keys are unique, in case your app interacts with other apps.- The
startActivity()
method starts an instance of theDisplayMessageActivity
that's specified by theIntent
. Next, you need to create that class.
0 Comments