StartActivityForResult

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Android StartActivityForResult Example

By the help of android startActivityForResult() method, we can get result from another activity.

By the help of android startActivityForResult() method, we can send information from one activity to another and vice-versa. The android startActivityForResult method, requires a result from the second activity (activity to be invoked).

In such case, we need to override the onActivityResult method that is invoked automatically when second activity returns result.

Method Signature

There are two variants of startActivityForResult() method.

  1. public void startActivityForResult (Intent intent, int requestCode)  

  2. public void startActivityForResult (Intent intent, int requestCode, Bundle options) 

Android StartActivityForResult Example

Let's see the simple example of android startActivityForResult method.

activity_main.xml

Drag one textview and one button from the pallete, now the xml file will look like this.

File: activity_main.xml
 
  1. <RelativeLayout xmlns:androclass="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.     tools:context=".MainActivity" >  
  10.     <TextView  
  11.         android:id="@+id/textView1"  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:layout_alignLeft="@+id/button1"  
  15.         android:layout_alignParentTop="true"  
  16.         android:layout_marginTop="48dp"  
  17.         android:text="Default Message" />  
  18.     <Button  
  19.         android:id="@+id/button1"  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:layout_below="@+id/textView1"  
  23.         android:layout_centerHorizontal="true"  
  24.         android:layout_marginTop="42dp"  
  25.         android:text="GetMessage" />  
  26. </RelativeLayout>  

second_main.xml

This xml file is created automatically when you create another activity. To create new activity Right click on the package inside the src -> New -> Other ->Android Activity.

Now drag one editText, one textView and one button from the pallete, now the xml file will look like this:

File: second_main.xml
 
  1. <RelativeLayout xmlns:androclass="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.     tools:context=".SecondActivity" >  
  10.     <EditText  
  11.         android:id="@+id/editText1"  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:layout_alignParentTop="true"  
  15.         android:layout_marginTop="61dp"  
  16.         android:layout_toRightOf="@+id/textView1"  
  17.         android:ems="10" />  
  18.     <TextView  
  19.         android:id="@+id/textView1"  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:layout_alignBaseline="@+id/editText1"  
  23.         android:layout_alignBottom="@+id/editText1"  
  24.         android:layout_alignParentLeft="true"  
  25.         android:text="Enter Message:" />  
  26.     <Button  
  27.         android:id="@+id/button1"  
  28.         android:layout_width="wrap_content"  
  29.         android:layout_height="wrap_content"  
  30.         android:layout_below="@+id/editText1"  
  31.         android:layout_centerHorizontal="true"  
  32.         android:layout_marginTop="34dp"  
  33.         android:text="Submit" />  
  34. </RelativeLayout>  

Activity class

Now let's write the code that invokes another activity and get result from that activity.

File: MainActivity.java

  1. package com.javatpoint.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. public class MainActivity extends Activity {  

  11.     TextView textView1;  

  12.     Button button1;  

  13.     @Override  

  14.     protected void onCreate(Bundle savedInstanceState) {  

  15.         super.onCreate(savedInstanceState);  

  16.         setContentView(R.layout.activity_main);  

  17.         textView1=(TextView)findViewById(R.id.textView1);  

  18.         button1=(Button)findViewById(R.id.button1);  

  19.         button1.setOnClickListener(new OnClickListener() {  

  20.             @Override  

  21.             public void onClick(View arg0) {  

  22.                 Intent intent=new Intent(MainActivity.this,SecondActivity.class);  

  23.                 startActivityForResult(intent, 2);// Activity is started with requestCode 2  

  24.             }  

  25.         });  

  26.     }  

  27.  // Call Back method  to get the Message form other Activity  

  28.     @Override  

  29.        protected void onActivityResult(int requestCode, int resultCode, Intent data)  

  30.        {  

  31.                  super.onActivityResult(requestCode, resultCode, data);  

  32.                   // check if the request code is same as what is passed  here it is 2  

  33.                    if(requestCode==2)  

  34.                          {  

  35.                             String message=data.getStringExtra("MESSAGE");   

  36.                             textView1.setText(message);  

  37.                          }  

  38.      }  

  39.     @Override  

  40.     public boolean onCreateOptionsMenu(Menu menu) {  

  41.         // Inflate the menu; this adds items to the action bar if it is present.  

  42.         getMenuInflater().inflate(R.menu.main, menu);  

  43.         return true;  

  44.     }  

  45. }  

SecondActivity class

Let's write the code that displays the content of second activity layout file.

File: SecondActivity.java

  1. package com.javatpoint.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 SecondActivity extends Activity {  

  12.     EditText editText1;  

  13.     Button button1;  

  14.     @Override  

  15.     protected void onCreate(Bundle savedInstanceState) {  

  16.         super.onCreate(savedInstanceState);  

  17.         setContentView(R.layout.activity_second);  

  18.         editText1=(EditText)findViewById(R.id.editText1);  

  19.             button1=(Button)findViewById(R.id.button1);  

  20.             button1.setOnClickListener(new OnClickListener() {  

  21.                 @Override  

  22.                 public void onClick(View arg0) {  

  23.                     String message=editText1.getText().toString();  

  24.                     Intent intent=new Intent();  

  25.                     intent.putExtra("MESSAGE",message);  

  26.                     setResult(2,intent);  

  27.                     finish();//finishing activity  

  28.                 }  

  29.             });  

  30.     }  

  31.     @Override  

  32.     public boolean onCreateOptionsMenu(Menu menu) {  

  33.         // Inflate the menu; this adds items to the action bar if it is present.  

  34.         getMenuInflater().inflate(R.menu.second, menu);  

  35.         return true;  

  36.     }  

  37. }  

Output:

android startactivityforresult example output 1 android startactivityforresult example output 2

android startactivityforresult example output 3 android startactivityforresult example output 4