1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.example.android.apis.app;
18
19// Need the following import to get access to the app resources, since this
20// class is in a sub-package.
21import com.example.android.apis.R;
22
23import android.app.Activity;
24import android.content.Intent;
25import android.text.Editable;
26import android.os.Bundle;
27import android.view.View;
28import android.view.View.OnClickListener;
29import android.widget.Button;
30import android.widget.TextView;
31
32/**
33 * Shows how an activity can send data to its launching activity when done.y.
34 * <p>This can be used, for example, to implement a dialog alowing the user to
35pick an e-mail address or image -- the picking activity sends the selected
36data back to the originating activity when done.</p>
37
38<p>The example here is composed of two activities: ReceiveResult launches
39the picking activity and receives its results; SendResult allows the user
40to pick something and sends the selection back to its caller.  Implementing
41this functionality involves the
42{@link android.app.Activity#setResult setResult()} method for sending a
43result and
44{@link android.app.Activity#onActivityResult onActivityResult()} to
45receive it.</p>
46
47<h4>Demo</h4>
48App/Activity/Receive Result
49
50<h4>Source files</h4>
51<table class="LinkTable">
52        <tr>
53            <td class="LinkColumn">src/com.example.android.apis/app/ReceiveResult.java</td>
54            <td class="DescrColumn">Launches pick activity and receives its result</td>
55        </tr>
56        <tr>
57            <td class="LinkColumn">src/com.example.android.apis/app/SendResult.java</td>
58            <td class="DescrColumn">Allows user to pick an option and sends it back to its caller</td>
59        </tr>
60        <tr>
61            <td class="LinkColumn">/res/any/layout/receive_result.xml</td>
62            <td class="DescrColumn">Defines contents of the ReceiveResult screen</td>
63        </tr>
64        <tr>
65            <td class="LinkColumn">/res/any/layout/send_result.xml</td>
66            <td class="DescrColumn">Defines contents of the SendResult screen</td>
67        </tr>
68</table>
69
70 */
71public class ReceiveResult extends Activity {
72    /**
73     * Initialization of the Activity after it is first created.  Must at least
74     * call {@link android.app.Activity#setContentView setContentView()} to
75     * describe what is to be displayed in the screen.
76     */
77    @Override
78	protected void onCreate(Bundle savedInstanceState) {
79        // Be sure to call the super class.
80        super.onCreate(savedInstanceState);
81
82        // See assets/res/any/layout/hello_world.xml for this
83        // view layout definition, which is being set here as
84        // the content of our screen.
85        setContentView(R.layout.receive_result);
86
87        // Retrieve the TextView widget that will display results.
88        mResults = (TextView)findViewById(R.id.results);
89
90        // This allows us to later extend the text buffer.
91        mResults.setText(mResults.getText(), TextView.BufferType.EDITABLE);
92
93        // Watch for button clicks.
94        Button getButton = (Button)findViewById(R.id.get);
95        getButton.setOnClickListener(mGetListener);
96    }
97
98    /**
99     * This method is called when the sending activity has finished, with the
100     * result it supplied.
101     *
102     * @param requestCode The original request code as given to
103     *                    startActivity().
104     * @param resultCode From sending activity as per setResult().
105     * @param data From sending activity as per setResult().
106     */
107    @Override
108	protected void onActivityResult(int requestCode, int resultCode,
109		Intent data) {
110        // You can use the requestCode to select between multiple child
111        // activities you may have started.  Here there is only one thing
112        // we launch.
113        if (requestCode == GET_CODE) {
114
115            // We will be adding to our text.
116            Editable text = (Editable)mResults.getText();
117
118            // This is a standard resultCode that is sent back if the
119            // activity doesn't supply an explicit result.  It will also
120            // be returned if the activity failed to launch.
121            if (resultCode == RESULT_CANCELED) {
122                text.append("(cancelled)");
123
124            // Our protocol with the sending activity is that it will send
125            // text in 'data' as its result.
126            } else {
127                text.append("(okay ");
128                text.append(Integer.toString(resultCode));
129                text.append(") ");
130                if (data != null) {
131                    text.append(data.getAction());
132                }
133            }
134
135            text.append("\n");
136        }
137    }
138
139    // Definition of the one requestCode we use for receiving resuls.
140    static final private int GET_CODE = 0;
141
142    private OnClickListener mGetListener = new OnClickListener() {
143        public void onClick(View v) {
144            // Start the activity whose result we want to retrieve.  The
145            // result will come back with request code GET_CODE.
146            Intent intent = new Intent(ReceiveResult.this, SendResult.class);
147            startActivityForResult(intent, GET_CODE);
148        }
149    };
150
151    private TextView mResults;
152}
153
154