Reputation: 35
I am trying to follow the lesson here and now im stuck on "Building an Intent". I am quite confused for how to make this Intent and where to paste it. Can someone show me the step by step process on this tutorial? I am getting massive headaches now. Please I want to learn to do this.
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 most often they’re used to start another activity.
Inside the sendMessage() method, create an Intent to start an activity called DisplayMessageActivity:
Intent intent = new Intent(this, DisplayMessageActivity.class); The constructor used here takes two parameters:
A Context as its first parameter (this is used because the Activity class is a subclass of Context) The Class of the app component to which the system should deliver the Intent (in this case, the activity that should be started)
Upvotes: 0
Views: 2593
Reputation: 48602
How do i create Intent and to where to put the codes?
You want to open the activity using intent then you can write your code in this method.
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
Upvotes: 0
Reputation: 76001
As the tutorial says, you need to add the line of code that creates an new instance of the Intent
class. You will use this instance later to tell the OS to launch another activity or a service. In this particular example, the Intent
you are building will direct the OS to launch the DisplayMessageActivity
.
To do this step properly, you need to modify the sendMessage method that you have added in the previous step of the tutorial. The final method should look something like this:
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
}
After creating the Intent
, the code will take the content of the editText
control in the current activity, assign it to the message
variable, and then add it as an additional parameter to the intent
, so that the target DisplayMessageActivity
activity can do something with it.
Don't worry about the DisplayMessageActivity yet. It will be added in a later step.
Upvotes: 1