Android StartActivityForResult example tutorial

Send-Receive information back to second application activity to first activity using StartActivityForResult method.

StartActivityForResult is used to receive result back from android activity like if your starting camera application into your mobile phone then as the final result you want to display your captured photo on mobile phone screen. So here is the complete step by step tutorial for Android StartActivityForResult example tutorial.

android-project-download-code-button

Android StartActivityForResult example tutorial.

Code for MainActivity.java file.

 package com.startactivityforresult_android_examples.com;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

 EditText SendingName;
 Button SendName;
 TextView HoldName;
 String Name;
 Intent intent;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 SendingName = (EditText)findViewById(R.id.editText1);
 SendName = (Button)findViewById(R.id.button1);
 HoldName = (TextView)findViewById(R.id.textView1);
 
 SendName.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 Name = SendingName.getText().toString();
 intent = new Intent(getApplicationContext(),SecondActivity.class);
 intent.putExtra("name", Name);
 startActivityForResult(intent, 1);
 
 }
 });
 }
 
 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 
 if(resultCode == RESULT_OK){ 
 HoldName.setText(data.getStringExtra("ActivityResult")); 
 }
 
 }
}

Code for activity_main.xml layout file.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.startactivityforresult_android_examples.com.MainActivity" >

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/editText1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="20dp"
 android:text="Send Name to Second Activity" />

 <EditText
 android:id="@+id/editText1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="160dp"
 android:ems="10"
 android:gravity="center"
 android:hint="Enter Name" >

 <requestFocus />
 </EditText>

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/button1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="24dp"
 android:text="Receive Name from second Activity Sets Here"
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:gravity="center" />

 <TextView
 android:id="@+id/textView2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="16dp"
 android:text="FIRST ACTIVITY"
 android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

 

Code for SecondActivity.java file.

 package com.startactivityforresult_android_examples.com;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class SecondActivity extends Activity {

 TextView one;
 Button Back;
 String ActivityResult;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_second);
 
 one = (TextView)findViewById(R.id.textView1);
 Back = (Button)findViewById(R.id.button1);
 Intent intent = getIntent();
 one.setText(getIntent().getStringExtra("name"));
 ActivityResult = getIntent().getStringExtra("name");
 
 Back.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 Intent BackIntent = new Intent();
 BackIntent.putExtra("ActivityResult",ActivityResult);
 setResult(RESULT_OK,BackIntent); 
 finish();
 }
 });
 
 
 }
}

Code for activity_Second.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.startactivityforresult_android_examples.com.SecondActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="Large Text"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textView1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="32dp"
 android:text="Send Name Back to MainActivity" />

 <TextView
 android:id="@+id/textView2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="20dp"
 android:text="SECOND ACTIVITY"
 android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Screenshots:

Android StartActivityForResult example tutorial

Click Here to download Android StartActivityForResult example tutorial project.

Leave a Reply

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