Android Development and talk

Hour 2 – Understanding an Android Activity

with 2 comments

This is a correction to the code in Hour 2.

In Listing 2.12, we handle the result from a call to startActivityForResult().
The idea is that one Activity will handle data entered in a second Activity.

To show this, the code updates a TextView in the initial Activity.  Unfortunately, when I transcribed the code for the book I left out the initialization of that TextView.  The error is also in the code for download.  There were great tech reviewer and editors on this book.  This is my mistake and it will be corrected.

The required line that was dropped is:

      t = (TextView) findViewById (;

package com.bffmedia.hour2app;

import android.os.Bundle;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ActivityA extends Activity {
	TextView t;
    protected void onCreate(Bundle savedInstanceState) {
        Button b =  (Button)findViewById(;
        t = (TextView) findViewById (;
        b.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	Intent intent = new Intent(ActivityA.this, ActivityB.class);
                Bundle b = new Bundle();
            	b.putString("GREETING", "Hello");
            	intent.putExtra("BUNDLE", b);
            	intent.putExtra("STRING", "World!");
            	intent.putExtra("BOOLEAN", true);
            	intent.putExtra("INT", 5);

        Button getData =  (Button)findViewById(;
        getData.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	Intent intent = new Intent(ActivityA.this, ActivityC.class);
            	startActivityForResult(intent, 0);           	


    protected void onActivityResult(int requestCode, int resultCode, Intent data ) {
    	if (requestCode == 0 && resultCode == Activity.RESULT_OK){
    		String enteredData = data.getStringExtra("Data");
        super.onActivityResult(requestCode, resultCode, data);

    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(, menu);
        return true;


Written by Carmen Delessio

December 2, 2013 at 3:31 am

2 Responses

Subscribe to comments with RSS.

  1. Listing 2.7, Line 10: “intnumItems” should be “int numItems”


    January 18, 2014 at 2:10 pm

  2. There is another inconsistency in your posted correction:

    activity_layout != activity_a_layout

    getMenuInflater().inflate(, menu);

    The book doesn’t say to change the initial ‘activity_layout’ when ‘activity_c_layout’ is created.
    I would suggest sticking with activity_layout.

    Suggested Fix:
    Line 17: (on pasted corrected code above) setContentView(R.layout.activity_layout);


    January 19, 2014 at 10:20 am

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.