특정 화면을 진행하다가 보면 해당 화면의 결과를 해당 화면을 콜했던 화면에서 받아봐야 할 경우가 있다. 그 때에 사용하는 onActivityResult(int i, int i) 메소드가 있다.
그리고 특정 값을 넘기는 방법도 있는데 이 부분은 추후 진행하겠다.
소스는 Android sample로 진행한다.
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:
17: package 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.
21: import java.util.Map;
22:
23: import com.example.android.apis.R;
24:
25: import android.app.Activity;
26: import android.content.Intent;
27: import android.text.Editable;
28: import android.os.Bundle;
29: import android.view.View;
30: import android.view.View.OnClickListener;
31: import android.widget.Button;
32: import android.widget.TextView;
33:
34: import java.util.Map;
35:
36: /**
37: * Shows how an activity can send data to its launching activity when done.y.
38: * <p>This can be used, for example, to implement a dialog alowing the user to
39: pick an e-mail address or image -- the picking activity sends the selected
40: data back to the originating activity when done.</p>
41:
42: <p>The example here is composed of two activities: ReceiveResult launches
43: the picking activity and receives its results; SendResult allows the user
44: to pick something and sends the selection back to its caller. Implementing
45: this functionality involves the
46: {@link android.app.Activity#setResult setResult()} method for sending a
47: result and
48: {@link android.app.Activity#onActivityResult onActivityResult()} to
49: receive it.</p>
50:
51: <h4>Demo</h4>
52: App/Activity/Receive Result
53:
54: <h4>Source files</h4>
55: <table class="LinkTable">
56: <tr>
57: <td class="LinkColumn">src/com.example.android.apis/app/ReceiveResult.java</td>
58: <td class="DescrColumn">Launches pick activity and receives its result</td>
59: </tr>
60: <tr>
61: <td class="LinkColumn">src/com.example.android.apis/app/SendResult.java</td>
62: <td class="DescrColumn">Allows user to pick an option and sends it back to its caller</td>
63: </tr>
64: <tr>
65: <td class="LinkColumn">/res/any/layout/receive_result.xml</td>
66: <td class="DescrColumn">Defines contents of the ReceiveResult screen</td>
67: </tr>
68: <tr>
69: <td class="LinkColumn">/res/any/layout/send_result.xml</td>
70: <td class="DescrColumn">Defines contents of the SendResult screen</td>
71: </tr>
72: </table>
73:
74: */
75: public class ReceiveResult extends Activity {
76: /**
77: * Initialization of the Activity after it is first created. Must at least
78: * call {@link android.app.Activity#setContentView setContentView()} to
79: * describe what is to be displayed in the screen.
80: */
81: @Override
82: protected void onCreate(Bundle savedInstanceState) {
83: // Be sure to call the super class.
84: super.onCreate(savedInstanceState);
85:
86: // See assets/res/any/layout/hello_world.xml for this
87: // view layout definition, which is being set here as
88: // the content of our screen.
89: setContentView(R.layout.receive_result);
90:
91: // Retrieve the TextView widget that will display results.
92: mResults = (TextView)findViewById(R.id.results);
93:
94: // This allows us to later extend the text buffer.
95: mResults.setText(mResults.getText(), TextView.BufferType.EDITABLE);
96:
97: // Watch for button clicks.
98: Button getButton = (Button)findViewById(R.id.get);
99: getButton.setOnClickListener(mGetListener);
100: }
101:
102: /**
103: * This method is called when the sending activity has finished, with the
104: * result it supplied.
105: *
106: * @param requestCode The original request code as given to
107: * startActivity().
108: * @param resultCode From sending activity as per setResult().
109: * @param data From sending activity as per setResult().
110: */
111: @Override
112: protected void onActivityResult(int requestCode, int resultCode,
113: Intent data) {
114: // You can use the requestCode to select between multiple child
115: // activities you may have started. Here there is only one thing
116: // we launch.
117: if (requestCode == GET_CODE) {
118:
119: // We will be adding to our text.
120: Editable text = (Editable)mResults.getText();
121:
122: // This is a standard resultCode that is sent back if the
123: // activity doesn't supply an explicit result. It will also
124: // be returned if the activity failed to launch.
125: if (resultCode == RESULT_CANCELED) {
126: text.append("(cancelled)");
127:
128: // Our protocol with the sending activity is that it will send
129: // text in 'data' as its result.
130: } else {
131: text.append("(okay ");
132: text.append(Integer.toString(resultCode));
133: text.append(") ");
134: if (data != null) {
135: text.append(data.getAction());
136: }
137: }
138:
139: text.append("\n");
140: }
141: }
142:
143: // Definition of the one requestCode we use for receiving resuls.
144: static final private int GET_CODE = 0;
145:
146: private OnClickListener mGetListener = new OnClickListener() {
147: public void onClick(View v) {
148: // Start the activity whose result we want to retrieve. The
149: // result will come back with request code GET_CODE.
150: Intent intent = new Intent(ReceiveResult.this, SendResult.class);
151: startActivityForResult(intent, GET_CODE);
152: }
153: };
154:
155: private TextView mResults;
156: }
157: