Recipe Passing Primitive Data Types Between Activities

Sometimes data needs to be passed to a launched activity. Sometimes a launched activity creates data that needs to be passed back to the calling activity. For example, a final score of a game needs to be returned to a high-scores screen.The different ways to pass information between activities are

■ Declare the relevant variable in the calling activity (for example, public int finalScore) and set it in the launched activity (for example, CallingActivity.finalScore=score).

■ Attach extras onto Bundles (demonstrated here).

■ Use Preferences to store data to be retrieved later (covered in Chapter 5,"User Interface Events").

■ Use the SQLite database to store data to be retrieved later (covered in Chapter 9).

A Bundle is a mapping from String values to various parcelable types. It can be created by adding extras to an intent. The following example shows data being passed from the main activity to the launched activity, where it is modified and passed back.

The variables (in this case, an integer and a String) are declared in the StartScreen activity. When the intent is created to call the PlayGame class, these variables are attached to the intent using the putExtra method.When the result is returned from the called activity, the variables can be read using the getExtras method. These calls are shown in Listing 2.16.

Listing 2.16 src/com/cookbook/passing_data_activities/StartScreen.java package com.cookbook.passing_data_activities;

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

public class StartScreen extends Activity { private static final int PLAY_GAME = 1010;

private TextView tv;

private int meaningOfLife = 42;

private String userName = "Douglas Adams";

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

tv = (TextView) findViewById(R.id.startscreen_text); //display initial values tv.setText(userName + ":" + meaningOfLife); //setup button listener

Button startButton = (Button) findViewById(R.id.play_game); startButton.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { startGame();

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == PLAY_GAME && resultCode == RESULT_OK) { meaningOfLife = data.getExtras().getInt("returnInt"); userName = data.getExtras().getString("userName"); //show it has changed tv.setText(userName + ":" + meaningOfLife);

super.onActivityResult(requestCode, resultCode, data);

private void startGame() {

Intent launchGame = new Intent(this, PlayGame.class);

//passing information to launched activity launchGame.putExtra("meaningOfLife", meaningOfLife); launchGame.putExtra("userName", userName);

startActivityForResult(launchGame, PLAY_GAME);

The variables passed into the PlayGame activity can be read using the getIntExtra and getStringExtra methods.When the activity finishes and prepares an intent to return, the putExtra method can be used to return data back to the calling activity. These calls are shown in Listing 2.17.

Listing 2.17 src/com/cookbook/passing_data_activities/PlayGame.java package com.cookbook.passing_data_activities;

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

public class PlayGame extends Activity { private TextView tv2; int answer; String author;

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.game);

tv2 = (TextView) findViewById(R.id.game_text);

//reading information passed to this activity //Get the intent that started this activity Intent i = getIntent();

//returns -1 if not initialized by calling activity answer = i.getIntExtra("meaningOfLife", -1); //returns [] if not initialized by calling activity author = i.getStringExtra("userName");

//change values for an example of return answer = answer - 41; author = author + " Jr.";

//setup button listener

Button startButton = (Button) findViewById(R.id.end_game); startButton.setOnClickListener(new View.OnClickListener() { public void onClick(View view) {

//return information to calling activity

Intent i = getIntent(); i.putExtra("returnInt", answer); i.putExtra("returnStr", author);

setResult(RESULT_OK, i); finish();

This page intentionally left blank

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook


Post a comment