Free Android app covering all aspects of addiction, including prevention and treatment

Start an activity for a result

  • Written by  Clive

Start me up: Starting an activity to get a result

Starting activity for result - result iconSometimes, you may need to get data from another activity. That activity could be in your app or in another app.

You could use startActivityForResult() to get that data for you.

Here’s an example.

Let’s say your app needs to take a photo. All you need to do is send the instruction to the device’s camera app. It will take the photo and return it to your app. Using startActivityForResult() is an easy way of doing that.

You can see how we did it in our camera app tutorial.

In this tutorial, I’ll use a simple app to show you how to use startActivityForResult () to get data from another activity.

The tutorial app

We’ll use startActivityForResult() to request data from two activities that we start. One will return some data while the other activity will cancel the request.

Let’s have a look at how it’s done.

Starting an activity for result app screenshot

The MainActivity activity is on the left: Press the top button and a new activity is started and a string returned. The string is displayed in the text view of the right-hand screen

The MainActivity activity

The main activity has two buttons. A new activity is started when either button is clicked.

The buttons

Here’s the code for the first button:

The startActivityForResult() call in button one

Start the new activity defined in the intent

Note the following:

We create a new Intent. The intent has two parameters:

  • MainActivity.this – the context
  • SecondActivity.class – the activity that we want to start

We call startActivityForResult() which starts the new activity. It has two parameters:

  • intentGetScript – our new intent containing the activity that we want to start
  • REQUEST_CODE_ONE – a unique integer that will identify this request when it returns the data

Here’s the code for the second button:

The startActivityForResult() call in button two

Start the new activity defined in the intent

Note the following:

We create a new Intent which has two parameters:

  • MainActivity.this – the context
  • ThirdActivity.class – the activity that we want to start

We call startActivityForResult() which will start the new activity. It has two parameters:

  • intentGetScript – our new intent containing the activity that we want to start
  • REQUEST_CODE_TWO – a unique integer that will identify this request when it returns the data

Getting the result

We use the onActivityResult() method to process the returned data.

This is triggered when the activity we started has finished and returns the result.

Getting the result with onActivityResult()

When the activity we started has finished its work, it returns the result and triggers onActivityResult()

The onActivityResult() method receives three parameters.

Note the following:

  • requestCode – this is the unique code that we gave to the original request. It identifies the request that this result belongs to
  • resultCode – an integer value. It indicates the result of the request
  • data – this is the returning intent which contains the requested data, if any

The resultCode

The activity we started to get the requested data must return a result code when it has finished.

There are three standard results:

  • RESULT_OK – (-1) there were no problems
  • RESULT_CANCELLED – (0) the request was cancelled
  • RESULT_FIRST_USER – (1) your own custom value

You can also supply your own integer value starting from RESULT_FIRST_USER.

Processing the result

We have two possible requests for a result. Each has a unique ID, REQUEST_CODE_ONE and REQUEST_CODE_TWO.

We use a switch statement to filter the returned result, using the request code as the filter.

Here’s the code for the request identified by REQUEST_CODE_ONE:

Processing the result with a switch statement. case 101

We use an if statement to check whether the result was ok

If the result is OK then we get the string out of the intent and display it in the text view, textViewDisplay.

If the resultCode is anything but RESULT_OK, we test it to see if it matches RESULT_CANCELLED. If it matches RESULT_CANCELLED, then we display a Toast message.

Here’s the code for the request identified by REQUEST_CODE_TWO:

Processing the result with a switch statement. case 102

Processing the cancelled request

If the result is OK then we get the bundle out of the data intent and use an if statement to see if the bundle contains any data.

We display a Toast message if the resultCode matches RESULT_CANCELLED.

The new activities

Pressing the top button in the main activity starts the SecondActivity activity.

Here’s the second activity’s code:

Returning the result. RESULT_OK 

The code that returns the requested data

Whatever data you expect from the activity that you have started must be put into the intent that is returned. You can see how it's done in the Passing data between activities tutorial.

In our activity, we create a new intent and load it with a string. Then we set the result and call finish().  This returns the result and triggers the onActivityResult() method in the main activity where the result is processed.

I’ve added the boolean success for you to try out the RESULT_OK and RESULT_CANCELLED options. Change the value of success and run the app. See how the onActivityResult() handles the different results.

Pressing the bottom button in the main activity starts the ThirdActivity activity.

Here’s the third activity’s code:

Returning the result. RESULT_CANCELLED

The request is cancelled and an empty intent returned

We create a new, empty intent. Then we set the result to RESULT_CANCELLED and call finish(). Calling finish() returns the result and triggers the onActivityResult() method in the main activity where the result is processed.

I hope that you have found this tutorial helpful.

Please consider subscribing to our notification email. We’ll send you one email on Friday with links to our latest tutorials. That way you won’t miss out. If we didn’t publish any then we won’t send any email. No spam. 

This tutorial was created using Android Studio. You can download the project files here Download icon

Are you using Eclipse or another IDE? Here's how you can use this project's Android Studio files.