Lesson 28. Extras - Transmit Data Using Intent

Lesson 28. Extras – Transmit Data Using Intent

In this lesson:

– transmit data using Intent

In the past lessons we learned what it is Intent and how to use it. From one Activity we just called another, passing action. Now let’s learn to pass data. Let’s make a simple application. On the first screen we will enter our name and the second screen will display this information. transfer data we will inside the Intent.

let’s create project:

Project name: P0281_IntentExtras
Build Target: Android 2.3.3
Application name: IntentExtras
Package name: ru.startandroid.develop.p0281intentextras
Create Activity: MainActivity

open main.xml and draw a screen with fields and a submit button:


IN EditText we will enter a name and a button Submit will cause another screen to transmit this data.

We write code for MainActivity.java:

package ru.startandroid.develop.p0281intentextras;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements OnClickListener {
  EditText etFName;
  EditText etLName;
  Button btnSubmit;
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        etFName = (EditText) findViewById(R.id.etFName);
        etLName = (EditText) findViewById(R.id.etLName);
        btnSubmit = (Button) findViewById(R.id.btnSubmit);

  public void onClick(View v) {
    Intent intent = new Intent(this, ViewActivity.class); 
    intent.putExtra("fname", etFName.getText().toString());
    intent.putExtra("lname", etLName.getText().toString());

We define input fields and a button. We assign a button to the button – Activity (this). Consider the implementation of the method onClick. we we create Intent using class, not action. If you remember, this is how we started dating Intent. Recall – this means that the system will look through the manifest file of our application, and if it finds an Activity with this class – will display it. ViewActivity has not yet been created, so the code will be highlighted in red. This does not prevent us from saving the file. A little later we will create this Activity and the error will disappear.

So Intent created, let’s see the code below. The putExtra method is used. It has many variations and a similar method way for MapThat is adds to the object a couple. The first parameter is key(Name), the second is value.

We have placed two objects named Intent: fname and lname. fname contains the value of the etFName field, lname is the value of the etLName field. only remains Send stocked Intent using the method startActivity.

Now let’s create a friend Activity. let’s call it ViewActivity.

Create a layout file for him view.xml:


Here is just a TextView that will display the data you have received.

we create a class ViewActivity. And we write the code:

package ru.startandroid.develop.p0281intentextras;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class ViewActivity extends Activity {
  TextView tvView;
  protected void onCreate(Bundle savedInstanceState) {
    tvView = (TextView) findViewById(R.id.tvView);
    Intent intent = getIntent();
    String fName = intent.getStringExtra("fname");
    String lName = intent.getStringExtra("lname");
    tvView.setText("Your name is: " + fName + " " + lName);

We find TextView, then we get Intent and we pull out from it String objects with names fname and lname. These are the same values ​​we put in the MainActivity.java code. Form the output string in TextView using the received data.

Be sure to spell out ViewActivity in the manifest. This time we don’t need any Intent Filter because we know the name of the Activity class exactly and use an explicit call.

We’ll save everything and run it.

We see the following screen:

Fill in the fields as you wish. I will write John in field First Name and Smith in field Last Name.

Click Submit:

ViewActivity was displayed, read Intent data and displayed.

Not only String data can be placed in Intent. In the list of Intent methods, you can see the variety of types that putExtra can accept.

In the next lesson:

– we call Activity with return of result

Discuss in the forum [114 replies]

Leave a Comment