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.
- <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"
- android:background="#cf99ff"
- tools:context=".MainActivity" >
- <TextView
- android:id="@+id/txtView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentTop="true"
- android:layout_centerInParent="true"
- android:textSize="30dp"
- android:text="Message"
- android:textStyle="bold"/>
- <Button
- android:id="@+id/btn_get"
- android:layout_width="150dp"
- android:layout_height="80dp"
- android:layout_below="@+id/txtView1"
- android:layout_marginLeft="65dp"
- android:layout_marginTop="42dp"
- android:text="GetMessage" />
- </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.
- <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"
- android:background="#cf99ff"
- tools:context=".SecondActivity" >
- <EditText
- android:id="@+id/editText1"
- android:textStyle="bold"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="40dp"
- android:layout_alignParentTop="true"
- android:ems="10" />
- <TextView
- android:textStyle="bold"
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:text="Enter Message:" />
- <Button
- android:id="@+id/btn"
- android:textStyle="bold"
- android:layout_width="150dp"
- android:layout_height="70dp"
- android:layout_below="@+id/editText1"
- android:layout_marginTop="34dp"
- android:text="Submit" />
- </RelativeLayout>
Step 3
Create a Java class file with the following:
- package com.startactivityforresult;
- import android.os.Bundle;
- import android.app.Activity;
- import android.content.Intent;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
-
- public class MainActivity extends Activity
- {
- TextView txtview1;
- Button btn_get;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- txtview1 = (TextView) findViewById(R.id.txtView1);
- btn_get = (Button) findViewById(R.id.btn_get);
-
- btn_get.setOnClickListener(new OnClickListener()
- {
-
- @Override
- public void onClick(View arg0)
- {
-
- Intent i = new Intent(MainActivity.this, Second.class);
- startActivityForResult(i, 2);
- }
- });
- }
-
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data)
- {
- super.onActivityResult(requestCode, resultCode, data);
-
- if (requestCode == 2)
- {
- String message = data.getStringExtra("MESSAGE");
- txtview1.setText(message);
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu)
- {
-
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- }
Step 4
- package com.startactivityforresult;
- import android.os.Bundle;
- import android.app.Activity;
- import android.content.Intent;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- public class Second extends Activity
- {
- EditText edtText1;
- Button btn;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.second);
- edtText1 = (EditText) findViewById(R.id.editText1);
- btn = (Button) findViewById(R.id.btn);
- btn.setOnClickListener(new OnClickListener()
- {
- @Override
- public void onClick(View arg0)
- {
- String message = edtText1.getText().toString();
- Intent i = new Intent();
- i.putExtra("MESSAGE", message);
- setResult(2, i);
- finish();
- }
- });
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu)
- {
-
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- }
Step 5
The following is the Android Manifest.XML file
. In the Android Manifest.xml file, you will declare the second activity.
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.startactivityforresult"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="18" />
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.startactivityforresult.MainActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity
- android:name=".Second"></activity>
- </application>
- </manifest>
Step 6
After running your application:
Click on the button as in the following:
Enter the message
Your first Activity