FileDocCategorySizeDatePackage
ReceiveResult.javaAPI DocGoogle Android v1.5 Example5900Sun Nov 11 13:01:04 GMT 2007com.google.android.samples.app

ReceiveResult

public class ReceiveResult extends android.app.Activity
Shows how an activity can send data to its launching activity when done.y.

This can be used, for example, to implement a dialog alowing the user to pick an e-mail address or image -- the picking activity sends the selected data back to the originating activity when done.

The example here is composed of two activities: ReceiveResult launches the picking activity and receives its results; SendResult allows the user to pick something and sends the selection back to its caller. Implementing this functionality involves the {@link android.app.Activity#setResult setResult()} method for sending a result and {@link android.app.Activity#onActivityResult onActivityResult()} to receive it.

Demo

App/Activity/Receive Result

Source files

src/com/google/android/samples/app/ReceiveResult.java Launches pick activity and receives its result
src/com/google/android/samples/app/SendResult.java Allows user to pick an option and sends it back to its caller
/res/any/layout/receive_result.xml Defines contents of the ReceiveResult screen
/res/any/layout/send_result.xml Defines contents of the SendResult screen

Fields Summary
private static final int
GET_CODE
private android.view.View.OnClickListener
mGetListener
private android.widget.TextView
mResults
Constructors Summary
Methods Summary
protected voidonActivityResult(int requestCode, int resultCode, java.lang.String data, android.os.Bundle extras)
This method is called when the sending activity has finished, with the result it supplied.

param
requestCode The original request code as given to startActivity().
param
resultCode From sending activity as per setResult().
param
data From sending activity as per setResult().
param
extras From sending activity as per setResult().

        // You can use the requestCode to select between multiple child
        // activities you may have started.  Here there is only one thing
        // we launch.
        if (requestCode == GET_CODE) {

            // We will be adding to our text.
            Editable text = (Editable)mResults.getText();

            // This is a standard resultCode that is sent back if the
            // activity doesn't supply an explicit result.  It will also
            // be returned if the activity failed to launch.
            if (resultCode == RESULT_CANCELED) {
                text.append("(cancelled)");

            // Our protocol with the sending activity is that it will send
            // text in 'data' as its result.
            } else {
                text.append("(okay ");
                text.append(Integer.toString(resultCode));
                text.append(") ");
                if (data != null) {
                    text.append(data);
                }
            }

            text.append("\n");
        }
    
protected voidonCreate(android.os.Bundle icicle)
Initialization of the Activity after it is first created. Must at least call {@link android.app.Activity#setContentView setContentView()} to describe what is to be displayed in the screen.

        // Be sure to call the super class.
        super.onCreate(icicle);

        // See assets/res/any/layout/hello_world.xml for this
        // view layout definition, which is being set here as
        // the content of our screen.
        setContentView(R.layout.receive_result);

        // Retrieve the TextView widget that will display results.
        mResults = (TextView)findViewById(R.id.results);

        // This allows us to later extend the text buffer.
        mResults.setInputMethod(NullInputMethod.getInstance());

        // Watch for button clicks.
        Button getButton = (Button)findViewById(R.id.get);
        getButton.setOnClickListener(mGetListener);