Introduction

This article explains StartActvityForResult in Android.
StartActivityForResult
StartActivityForResult is a method for starting an activity for which you would like a result when it is finished. It is provided by the Activity class.
Step 1
Create an XML file with the following:. This is the activity_main file that consists of one text view and a button in a relative layout.
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:paddingBottom="@dimen/activity_vertical_margin"
  5. android:paddingLeft="@dimen/activity_horizontal_margin"
  6. android:paddingRight="@dimen/activity_horizontal_margin"
  7. android:paddingTop="@dimen/activity_vertical_margin"
  8. android:background="#cf99ff"
  9. tools:context=".MainActivity" >
  10. <TextView
  11. android:id="@+id/txtView1"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:layout_alignParentTop="true"
  15. android:layout_centerInParent="true"
  16. android:textSize="30dp"
  17. android:text="Message"
  18. android:textStyle="bold"/>
  19. <Button
  20. android:id="@+id/btn_get"
  21. android:layout_width="150dp"
  22. android:layout_height="80dp"
  23. android:layout_below="@+id/txtView1"
  24. android:layout_marginLeft="65dp"
  25. android:layout_marginTop="42dp"
  26. android:text="GetMessage" />
  27. </RelativeLayout>
Step 2
Create another XML file with the following: This is my second XML file that consists of an edittext, a textview, and a button.
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingBottom="@dimen/activity_vertical_margin"
  6. android:paddingLeft="@dimen/activity_horizontal_margin"
  7. android:paddingRight="@dimen/activity_horizontal_margin"
  8. android:paddingTop="@dimen/activity_vertical_margin"
  9. android:background="#cf99ff"
  10. tools:context=".SecondActivity" >
  11. <EditText
  12. android:id="@+id/editText1"
  13. android:textStyle="bold"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:layout_marginTop="40dp"
  17. android:layout_alignParentTop="true"
  18. android:ems="10" />
  19. <TextView
  20. android:textStyle="bold"
  21. android:id="@+id/textView1"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:layout_alignParentLeft="true"
  25. android:text="Enter Message:" />
  26. <Button
  27. android:id="@+id/btn"
  28. android:textStyle="bold"
  29. android:layout_width="150dp"
  30. android:layout_height="70dp"
  31. android:layout_below="@+id/editText1"
  32. android:layout_marginTop="34dp"
  33. android:text="Submit" />
  34. </RelativeLayout>
Step 3
Create a Java class file with the following:
  1. package com.startactivityforresult;
  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.view.Menu;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.Button;
  9. import android.widget.TextView;
  10. //This class extends the Activity class that provides some methods for the activity. In this i have set the button onItsClickListener, so on click another activity will be start and startActivityForResult() will the intent with request code.
  11. public class MainActivity extends Activity
  12. {
  13. TextView txtview1;
  14. Button btn_get;
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState)
  17. {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.activity_main);
  20. txtview1 = (TextView) findViewById(R.id.txtView1);
  21. btn_get = (Button) findViewById(R.id.btn_get);
  22. //button set on its click listener
  23. btn_get.setOnClickListener(new OnClickListener()
  24. {
  25. // when you click on the button onClick() will be invoke and startActivityForResult() will be invoke
  26. @Override
  27. public void onClick(View arg0)
  28. {
  29. //create the object of intent
  30. Intent i = new Intent(MainActivity.this, Second.class);
  31. startActivityForResult(i, 2); // Activity is started with requestCode 2
  32. }
  33. });
  34. }
  35. // This method is called when from the second Activity setResult() will be called by ing requestcode and an intent.
  36. @Override
  37. protected void onActivityResult(int requestCode, int resultCode, Intent data)
  38. {
  39. super.onActivityResult(requestCode, resultCode, data);
  40. // check if the request code is same as what is ed here it is 2
  41. if (requestCode == 2)
  42. {
  43. String message = data.getStringExtra("MESSAGE");
  44. txtview1.setText(message);
  45. }
  46. }
  47. @Override
  48. public boolean onCreateOptionsMenu(Menu menu)
  49. {
  50. // Inflate the menu; this adds items to the action bar if it is present.
  51. getMenuInflater().inflate(R.menu.main, menu);
  52. return true;
  53. }
  54. }
Step 4
  1. package com.startactivityforresult;
  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.view.Menu;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.TextView;
  11. public class Second extends Activity
  12. {
  13. EditText edtText1;
  14. Button btn;
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState)
  17. {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.second);
  20. edtText1 = (EditText) findViewById(R.id.editText1);
  21. btn = (Button) findViewById(R.id.btn);
  22. btn.setOnClickListener(new OnClickListener()
  23. {
  24. @Override
  25. public void onClick(View arg0)
  26. {
  27. String message = edtText1.getText().toString();
  28. Intent i = new Intent();
  29. i.putExtra("MESSAGE", message);
  30. setResult(2, i);
  31. finish(); //finishing activity
  32. }
  33. });
  34. }
  35. @Override
  36. public boolean onCreateOptionsMenu(Menu menu)
  37. {
  38. // Inflate the menu; this adds items to the action bar if it is present.
  39. getMenuInflater().inflate(R.menu.main, menu);
  40. return true;
  41. }
  42. }
Step 5
The following is the Android Manifest.XML file. In the Android Manifest.xml file, you will declare the second activity.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.startactivityforresult"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6. <uses-sdk
  7. android:minSdkVersion="8"
  8. android:targetSdkVersion="18" />
  9. <application
  10. android:allowBackup="true"
  11. android:icon="@drawable/ic_launcher"
  12. android:label="@string/app_name"
  13. android:theme="@style/AppTheme" >
  14. <activity
  15. android:name="com.startactivityforresult.MainActivity"
  16. android:label="@string/app_name" >
  17. <intent-filter>
  18. <action android:name="android.intent.action.MAIN" />
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. <activity
  23. android:name=".Second"></activity>
  24. </application>
  25. </manifest>
Step 6
After running your application:
Click on the button as in the following:
Enter the message
Your first Activity