4. Passing Data with Android Intent Extras

This entry is part 4 of 22 in the series Android-Intermediate

Passing Data Android Tutorial IconWorking with Android Intents to Pass Data

Passing data between Android activities is incredibly easy.  Simply put the data as the extras of an Android intent and set the result. Watch this tutorial or look at the source code to see a working example.  In this Android video tutorial we will also review what we have learned from the previous 3 tutorials. If you haven’t programmed with Android before, or if you think I’m moving a little fast in this tutorial, you should start with the Android basics series.


Android Intent Extras Walk-through and Review


Android Intents, Extras, and Data

Anytime you need to pass data between two activities you will need to work with an intent (as I’ve mentioned in the previous few tutorials).

You will be able to put extras into the intent so that the opening (or receiving) activity will be able to get the data from the intent through a variety of different methods.

Passing data is as easy as setting up your base intent, and adding extra data, by intent.putExtra(“yourExtraName”, yourExtraValue); and getting that data by intent.getStringExtra(“yourExtraName”);

I don’t feel like going into a lot of detail, because seriously getting and recieving data is incredibly easy, so if you want to see more, check out the video tutorial, or the source code below.


Source Code

The40SeriesActivity.java

package com.mybringback.theworks;

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.TextView;

public class The40SeriesActivity extends Activity implements OnClickListener {

	Button width, height, calc;

	TextView area;

	/** Called when the activity is first created. */

	@Override

	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.main);

		width = (Button) findViewById(R.id.button1);

		height = (Button) findViewById(R.id.button2);

		calc = (Button) findViewById(R.id.button3);

		area = (TextView) findViewById(R.id.textView1);

		width.setOnClickListener(this);

		height.setOnClickListener(this);

		calc.setOnClickListener(this);

	}

	@Override

	public void onClick(View v) {

		// TODO Auto-generated method stub

		Intent i = new Intent(this, Numbers.class);

		switch (v.getId()) {

		case R.id.button1:

			// width

			i.putExtra("numbers", "width");

			startActivityForResult(i, 1);

			break;

		case R.id.button2:

			// height

			i.putExtra("numbers", "height");

			startActivityForResult(i, 1);

			break;

		case R.id.button3:

			// calc

			int a = Integer.valueOf(width.getText().toString());

			int b = Integer.valueOf(height.getText().toString());

			area.setText(a*b+" sq ft");

			break;

		default:

			break;

		}

	}

	@Override

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		// TODO Auto-generated method stub

		super.onActivityResult(requestCode, resultCode, data);

		if(data.getExtras().containsKey("widthInfo")){

			width.setText(data.getStringExtra("widthInfo"));

		}

		if(data.getExtras().containsKey("heightInfo")){

			height.setText(data.getStringExtra("heightInfo"));

		}

	}

}

Numbers.java

package com.mybringback.theworks;

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 Numbers extends Activity implements OnClickListener {

	EditText number;

	Button sendInfo;

	@Override

	protected void onCreate(Bundle savedInstanceState) {

		// TODO Auto-generated method stub

		super.onCreate(savedInstanceState);

		setContentView(R.layout.numbers);

		number = (EditText) findViewById(R.id.editText1);

		sendInfo = (Button) findViewById(R.id.button1);

		sendInfo.setOnClickListener(this);

	}

	@Override

	public void onClick(View v) {

		// TODO Auto-generated method stub

		String s = number.getText().toString();

		Intent i = getIntent();

		String msg = i.getStringExtra("numbers");

		if (msg.contentEquals("width")) {

			i.putExtra("widthInfo", s);

			setResult(RESULT_OK, i);

			finish();

		}

		if (msg.contentEquals("height")) {

			i.putExtra("heightInfo", s);

			setResult(RESULT_OK, i);

			finish();

		}

	}

}

Navigation

Previous Lesson
Next Lesson

Author: trav

I'm just an average guy that love programming.

Share This Post On