1page.title=Getting a Result from an Activity 2page.tags=intents 3helpoutsWidget=true 4 5trainingnavtop=true 6 7@jd:body 8 9<div id="tb-wrapper"> 10 <div id="tb"> 11 12<h2>This lesson teaches you to</h2> 13<ol> 14 <li><a href="#StartActivity">Start the Activity</a></li> 15 <li><a href="#ReceiveResult">Receive the Result</a></li> 16</ol> 17 18<h2>You should also read</h2> 19<ul> 20 <li><a href="{@docRoot}training/sharing/index.html">Sharing Simple Data</a></li> 21 <li><a href="{@docRoot}training/secure-file-sharing/index.html">Sharing Files</a> 22</ul> 23 24 </div> 25</div> 26 27<p>Starting another activity doesn't have to be one-way. You can also start another activity and 28receive a result back. To receive a result, call {@link android.app.Activity#startActivityForResult 29startActivityForResult()} (instead of {@link android.app.Activity#startActivity 30startActivity()}).</p> 31 32<p>For example, your app can start a camera app and receive the captured photo as a result. Or, you 33might start the People app in order for the user to select a 34contact and you'll receive the contact details as a result.</p> 35 36<p>Of course, the activity that responds must be designed to return a result. When it does, it 37sends the result as another {@link android.content.Intent} object. Your activity receives it in 38the {@link android.app.Activity#onActivityResult onActivityResult()} callback.</p> 39 40<p class="note"><strong>Note:</strong> You can use explicit or implicit intents when you call 41{@link android.app.Activity#startActivityForResult startActivityForResult()}. When starting one of 42your own activities to receive a result, you should use an explicit intent to ensure that you 43receive the expected result.</p> 44 45 46<h2 id="StartActivity">Start the Activity</h2> 47 48<p>There's nothing special about the {@link android.content.Intent} object you use when starting 49an activity for a result, but you do need to pass an additional integer argument to the {@link 50android.app.Activity#startActivityForResult startActivityForResult()} method.</p> 51 52<p>The integer argument is a "request code" that identifies your request. When you receive the 53result {@link android.content.Intent}, the callback provides the same request code so that your 54app can properly identify the result and determine how to handle it.</p> 55 56<p>For example, here's how to start an activity that allows the user to pick a contact:</p> 57 58<pre> 59static final int PICK_CONTACT_REQUEST = 1; // The request code 60... 61private void pickContact() { 62 Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts")); 63 pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers 64 startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST); 65} 66</pre> 67 68 69<h2 id="ReceiveResult">Receive the Result</h2> 70 71<p>When the user is done with the subsequent activity and returns, the system calls your activity's 72{@link android.app.Activity#onActivityResult onActivityResult()} method. This method includes three 73arguments:</p> 74 75<ul> 76 <li>The request code you passed to {@link 77android.app.Activity#startActivityForResult startActivityForResult()}.</li> 78 <li>A result code specified by the second activity. This is either {@link 79android.app.Activity#RESULT_OK} if the operation was successful or {@link 80android.app.Activity#RESULT_CANCELED} if the user backed out or the operation failed for some 81reason.</li> 82 <li>An {@link android.content.Intent} that carries the result data.</li> 83</ul> 84 85<p>For example, here's how you can handle the result for the "pick a contact" intent:</p> 86 87<pre> 88@Override 89protected void onActivityResult(int requestCode, int resultCode, Intent data) { 90 // Check which request we're responding to 91 if (requestCode == PICK_CONTACT_REQUEST) { 92 // Make sure the request was successful 93 if (resultCode == RESULT_OK) { 94 // The user picked a contact. 95 // The Intent's data Uri identifies which contact was selected. 96 97 // Do something with the contact here (bigger example below) 98 } 99 } 100} 101</pre> 102 103<p>In this example, the result {@link android.content.Intent} returned by 104Android's Contacts or People app provides a content {@link android.net.Uri} that identifies the 105contact the user selected.</p> 106 107<p>In order to successfully handle the result, you must understand what the format of the result 108{@link android.content.Intent} will be. Doing so is easy when the activity returning a result is 109one of your own activities. Apps included with the Android platform offer their own APIs that 110you can count on for specific result data. For instance, the People app always returns a result 111with the content URI that identifies the selected contact, and the 112Camera app returns a {@link android.graphics.Bitmap} in the {@code "data"} extra (see the class 113about <a href="{@docRoot}training/camera/index.html">Capturing Photos</a>).</p> 114 115 116<h4>Bonus: Read the contact data</h4> 117 118<p>The code above showing how to get a result from the People app doesn't go into 119details about how to actually read the data from the result, because it requires more advanced 120discussion about <a href="{@docRoot}guide/topics/providers/content-providers.html">content 121providers</a>. However, if you're curious, here's some more code that shows how to query the 122result data to get the phone number from the selected contact:</p> 123 124<pre> 125@Override 126protected void onActivityResult(int requestCode, int resultCode, Intent data) { 127 // Check which request it is that we're responding to 128 if (requestCode == PICK_CONTACT_REQUEST) { 129 // Make sure the request was successful 130 if (resultCode == RESULT_OK) { 131 // Get the URI that points to the selected contact 132 Uri contactUri = data.getData(); 133 // We only need the NUMBER column, because there will be only one row in the result 134 String[] projection = {Phone.NUMBER}; 135 136 // Perform the query on the contact to get the NUMBER column 137 // We don't need a selection or sort order (there's only one result for the given URI) 138 // CAUTION: The query() method should be called from a separate thread to avoid blocking 139 // your app's UI thread. (For simplicity of the sample, this code doesn't do that.) 140 // Consider using {@link android.content.CursorLoader} to perform the query. 141 Cursor cursor = getContentResolver() 142 .query(contactUri, projection, null, null, null); 143 cursor.moveToFirst(); 144 145 // Retrieve the phone number from the NUMBER column 146 int column = cursor.getColumnIndex(Phone.NUMBER); 147 String number = cursor.getString(column); 148 149 // Do something with the phone number... 150 } 151 } 152} 153</pre> 154 155<p class="note"><strong>Note:</strong> Before Android 2.3 (API level 9), performing a query 156on the {@link android.provider.ContactsContract.Contacts Contacts Provider} (like the one shown 157above) requires that your app declare the {@link 158android.Manifest.permission#READ_CONTACTS} permission (see <a 159href="{@docRoot}guide/topics/security/security.html">Security and Permissions</a>). However, 160beginning with Android 2.3, the Contacts/People app grants your app a temporary 161permission to read from the Contacts Provider when it returns you a result. The temporary permission 162applies only to the specific contact requested, so you cannot query a contact other than the one 163specified by the intent's {@link android.net.Uri}, unless you do declare the {@link 164android.Manifest.permission#READ_CONTACTS} permission.</p> 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180