articles

Home / DeveloperSection / Articles / Getting a Result from an Activity

Getting a Result from an Activity

Anonymous User 4882 17-Nov-2014

Previously we see how to communicate between two activities in Android( Interacting one app to other app in android ). Here we see how to get result from an Android activity

Sometimes starting an activity doesn’t have to be one-way, i.e. we can start another activity and receives result back. To receive a result, call startActivityForResult() (instead of startActivity()). For e.g. our app can start a camera and receive the captured photo as a results.

Of course, the activity that responds must be designed to return a result. When it does, it sends the result as another onActivityResult() callback.

Start the Activity

We need to pass an additional integer argument to the startActivityForResult() method.The integer argument is a “request code” that identifies the request. When we receive the result Intent, the callback provides the same request code so that the app can properly identify the result and determine how to handle it.

For Example, here’s how to start an activity that allow a user to pick a contact:

Static final int PICK_CONTACT_REQUEST   = 1; // The request code

……
Private void pickContact() {
   Intnet pickContactIntent = new Intent( Intnet.ACTION_PICK, Uri.parse(“content : //contacts”));
  startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}

 Receive the Result

When the user is done with the subsequent activity and returns, the system calls our activity’s onActivityResult() method. This method incudes three arguments:

·    The request code we pass to startActivityFortResult()

·    A request code specified by the second activity. This is either RESULT_OK if the operation was successful or RESULT_CANCELED if the user backed out or the operation failed for the some reason.

·    An Intent that carries the result data.

For e.g. here’s how we can handle the result for the “pick a contact” intent:

@Override 
protected void onActivityResult( int requestCode, int resultCode, Intent data) {
//Check which request we’re responding to
If(requestcode == RESULT_OK) {
 // Make sure the request we successful
   If(resultCode == RESULT_OK) {
       // The user picked a contact.
        // The Intent’s data Uri identifies which contact was selected.
           // Do something with the contact here (bigger example below)
   }
}


The result Intent returned by android’s Contacts or people app provides a content Uri that identifies the contact the user selected.

Read the contact Data

Here’s the code that shows how to query the result data to get the phone number from the the selected contact:

@Override
Protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// check which request it is that we’re responding to
     If(requestCode == PICK_CONTACT_REQUEST) {
       // Make sure the request was successful
        If(resultcode == RESULT_OK) {
             // Get theURI that points to the selected contact
              Uri ContactUri = data.getData();
              // We only need the NUMBER column, because there will be only one row in the result
                String[] projection = {Phone.NUMBER};
                // Perform the query on the contact to get the NUMBER column
                 // We don't need a selection or sort order (there's only one result for the given URI)
                 // CAUTION: The query() method should be called from a separate thread to avoid blocking
                 // your app's UI thread. (For simplicity of the sample, this code doesn't do that.)
                 // Consider using CursorLoader to perform the query.
                    Cursor cursor = getContnetREsolver().query(contentUri, projection, nul, null, null);
                     Cursor.moveToFirst();
                      // Retrieve the phone number from the NUMBER column
                     Int column = cursor.getColumnbIndex(Phone.NUMBER);
                         // Do something with the phone number..
                   }
                }
              }

 

 This post is referred from Google docs Getting a Result from an Activity

In my next post, we will see  the ways to save the data in Android - Saving data in Android: Saving in Key-Value Sets

 

 


I am a content writter !

Leave Comment

Comments

Liked By