2. Android StartActivityForResult Example

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

Mybringback Android Tutorials Icon  startActivityForResult Method for Android

Time to start passing data, let’s code!

Welcome to the 2nd Android tutorial. In this example we will learn about passing data between two activities with Android’s startActivityForResult method.   These can be activities you’ve created, or even Android’s native activities, such as the camera activity (or intent) for example. 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.


Project Setup Walkthrough


Android startActivityForResult() Tutorial

According to the Android Developers Page, “An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.”

So, in layman’s terms, intents are needed to send and receive data between two activities.  So, if your app needs to grab a picture from the user’s photo gallery you would need to create a bridge between those two activities, aka an intent.   Same goes for getting information from a different activity, you will have to use an intent.  Even if you just want to open up a new activity (but not get any data from it) you will still have to start that activity passing an intent. Example :  startActivity(intent);

Now if we need to get information from an activity we will have tell the intent we are looking for a result from the activity we are starting.  We will do this by using the startActivityForResult method.  This will tell the activity we are opening that we are waiting to receive some sort of data.  The activity we opened will send the result back to the previous activity via setResult method.

The last thing in the equation is catching the result, and that will be done by adding the onActivityResult method to the original activity.  We will learn more about that in the next tutorial, but it’s good to know how the system works.  It’s something like this:

Activity #1

  • Open activity #2, but tell it we are looking for some data, via the startActivityForResult method.
  • Wait for the result to come back from Activity #2 and catch the info, via the onActivityResult method.
Activity #2
  • Figure out what data to pass back, and set that as the result, via the setResult method.

That about sums it up!  The video tutorial may seem a bit confusing because we passing in some additional parameters, but I’ll review everything in the 4th tutorial in this series, so make sure you stick with it! Here’s some source code:


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();
		}
	}
}

Resources

Download links: I currently don’t have a zip file for this tutorial. If you have watched the video and working project, please zip it and send it to support@mybringback.com


Course Navigation

Previous Lesson
Next Lesson

Author: trav

I'm just an average guy that love programming.

Share This Post On

1 Comment

Submit a Comment

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>