Android Start Activity for Result Example

Android Start Activity for Result

Here we will learn how to use an  Android Start Activity for Result with Example, which will return some required results from user to it’s parent Activity. So let’s discuss about Android Start Activity for Result Example with Source Code. Here we will start a new activity from it’s parent Activity. when user will put some input data into that activity and close/finish that input activity, we will send notification to the parent activity with the input data. It is very simple.

Android Start Activity for Result

Starting Activities and Getting Results

The method startActivity(Intent) is used to start a new activity. But sometimes we want to get back results from an activity when it ends. For example, we may start an activity that lets the user input his/her name in an EditText, when it ends, it returns the name. To do this, you call the startActivityForResult(Intent, int) version with a second integer parameter identifying the call. The result will come back through ouronActivityResult(int, int, Intent) method.

When an activity ends, it can call setResult(int) to return data back to its parent. It must always supply a result code, which can be the standard results RESULT_CANCELED, RESULT_OK, or any custom values starting at RESULT_FIRST_USER. In addition, it can optionally return back an Intent containing any additional data it wants. All of this information appears back on the parent’s Activity.onActivityResult(), along with the integer identifier it originally supplied.

Note: If a child activity fails for any reason (such as crashing), the parent activity will receive a result with the code RESULT_CANCELED.

Create Project: Activities and Getting Results Example

1. Create a project with project name: ActivityResult

2. Fill Application Name: ActivityResult

3. Fill the Package Name as: techblogon.activityresult

4. I have used SDK version Android 4.0.3 and Eclipse Version Indigo. But you can use any version.

5. Add below 2 java files (MainActivity.Java and SecondActivity.Java) in you project’s /src folder.

  • MainActivity.Java

This activity is the base activity from which we will start another activity for getting result.

  •  SecondActivity.Java

This activity will show user an EditText. Once user put some data and press Submit button; then the activity will close and send the data to the parent Activity (MainActivity Class) using API setResult(2,intentMessage).


6. Add below 2 xml layout files (activity_main.xml and layout2.xml) in your project’s res/layout folder.

  • activity_main.xml

This is used in MainActivity.Java file for showing GUI to start a new Activity for result.

  • layout2.xml

This is used in SecondActivity.Java file for showing GUI for user input.

7. Add the below Manifest file (AndroidManifest.xml) in your project’s root folder.

  • AndroidManifest.xml

8. Build and Run the application, then observe the result.

Note: Also you can download the complete code and import the project as an Existing android project.

downloadDownload  Android Start Activity for Result Example from Here.

I hope this tutorial will help you at it’s best.


Post By SmrutiRanjan (57 Posts)

Working @ Samsung as a Project Lead for Android Smartphones. I have been blogging since 2008. Previously I was writing articles for other bloggers, but finally I have started my own blog-"Techblogon".I am also an active contributor for the blog-"Gadgets n Gizmos World". Job is my necessity, but blogging is my passion.

Website: → Techblogon


9 Responses to Android Start Activity for Result Example

  1. Ashok Kumar says:

    Hello Sir,
    I’m new to Android. This Site helps me a lot. Thanks for your work. Keep it going.

  2. Kasun Wickramanayake says:

    great article.helps me lot

  3. fahmidul says:

    Awesome & very helpful article boss. we (novice) need you. please help us, don’t stop writing.

  4. Prax says:

    Thanks a lot,Very useful post for beginners.Keep writing :) God Bless You…

  5. knvchowdary says:

    so nice….

  6. B Patel says:

    Excellent Tutorial. It helps me lot. God Bless You…..

  7. Krunal says:

    Great and easily understandable example. can you please give me some suggestions on my problem. My problem is that i need to use same functionality with non-activity class. i.e in your example class MainActivity is a non-activity class then how can i achieve this. please give your valuable suggestions.

  8. Aakash Jain says:

    Hi smrutiRanjan,

    If I have 10 activities and calling all of them in a flow from 1 to 10. Now from 10th activity I put some data and now I have to use that in my first activity then?

Leave a Reply

Your email address will not be published. Required fields are marked *

8 − five =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">