Hour 2 – Understanding an Android Activity

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 (R.id.textView1);

package com.bffmedia.hour2app;

import android.os.Bundle;
import android.app.Activity;
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(R.id.button1);
        t = (TextView) findViewById (R.id.textView1);
        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(R.id.button2);
        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(R.menu.activity_layout, menu);
        return true;


2 thoughts on “Hour 2 – Understanding an Android Activity

  1. There is another inconsistency in your posted correction:

    activity_layout != activity_a_layout

    getMenuInflater().inflate(R.menu.activity_layout, 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);

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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