Start Another Activity
After completing the previous lesson, you have an app that shows an activity (a single screen) with a text field and a button. In this lesson, you’ll add some code to
MainActivity
that starts a new activity to display the message when the user taps Send.
Note: This lesson expects you are using Android Studio 2.3 or higher.
Respond to the send button
Add a method in
MainActivity.java
that's called by the button as follows:- In the file app > java > com.example.myfirstapp > MainActivity.java, add the
sendMessage()
method stub as shown below:public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } /** Called when the user taps the Send button */ public void sendMessage(View view) { // Do something in response to button } }
You may see an error because Android Studio cannot resolve theView
class used as the method argument. So click to place your cursor on theView
declaration, and then perform a Quick Fix by pressing Alt + Enter (or Option + Enter on Mac). (If a menu appears, select Import class.) - Now return to the activity_main.xml file to call this method from the button:
- Click to select the button in the Layout Editor.
- In the Properties window, locate the onClick property and select sendMessage [MainActivity] from the drop-down list.
Now when the button is tapped, the system calls the
sendMessage()
method.
Take note of the details in this method that are required in order for the system to recognize it as compatible with the
android:onClick
attribute. Specifically, the method must declare the following:- Public access
- A void return value
- A
View
as the only parameter (it is theView
object that was clicked)
Next, you’ll fill in this method to read the contents of the text field and deliver that text to another activity.
Build an Intent
An
Intent
is an object that provides runtime binding between separate components, such as two activities. The Intent
represents an app’s "intent to do something." You can use intents for a wide variety of tasks, but in this lesson, your intent starts another activity.
In
MainActivity.java
, add the EXTRA_MESSAGE
constant and the sendMessage()
code, as shown here:public class MainActivity extends AppCompatActivity { public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } /** Called when the user taps the Send button */ public void sendMessage(View view) { Intent intent = new Intent(this, DisplayMessageActivity.class); EditText editText = (EditText) findViewById(R.id.editText); String message = editText.getText().toString(); intent.putExtra(EXTRA_MESSAGE, message); startActivity(intent); } }
Android Studio will again encounter Cannot resolve symbol errors, so press Alt + Enter (or Option + Return on Mac). Your imports should end up as the following:
import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText;
An error remains for
DisplayMessageActivity
, but that's okay; you'll fix that in the next section.
Here's what's going on in
sendMessage()
:- The
Intent
constructor takes two parameters:- A
Context
as its first parameter (this
is used because theActivity
class is a subclass ofContext
) - The
Class
of the app component to which the system should deliver theIntent
(in this case, the activity that should be started).
- A
- The
putExtra()
method adds theEditText
's value to the intent. AnIntent
can carry data types as key-value pairs called extras. Your key is a public constantEXTRA_MESSAGE
because the next activity uses the key to retrieve the text value. It's a good practice to define keys for intent extras using your app's package name as a prefix. This ensures the keys are unique, in case your app interacts with other apps. - The
startActivity()
method starts an instance of theDisplayMessageActivity
specified by theIntent
. Now you need to create that class
Comments
Post a Comment