You are on page 1of 8

Intent is the negotiator between two activities or between two applications.

When we start a new activity using Intent, the Intent can return some result to the current activity. So how we can return result from an intent, this post is all about.

The startActivity() method invokes another activity but does not return result to the current activity. For example, we have an activity that prompts the user for username & password. The information entered by the user in that activity needs to be passed to the calling activity for further processing. So if we need to pass data back from an activity, we need to use startActivityForResult() method.

Example Project :Project Name : ReturnResultFromIntent Package : com.sunil.rrfi Build Target : Android 2.2 Activities : Activity1 , Activity2 Layouts : main , layout2

main.xml (File : res/layout/main.xml) <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/btnStart" android:layout_marginTop="20dp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Start Activity For Results" /> </LinearLayout>

layout2.xml (File: res/layout/layout2.xml) <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Enter your username : " /> <EditText android:id="@+id/username" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btnOK" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="OK" /> </LinearLayout>

Activity1.java (File: Activity1.java) package com.sunil.rrfi; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class Activity1 extends Activity { /** Called when the activity is first created. */ int request_code = 1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

Button start = (Button) findViewById(R.id.btnStart); start.setOnClickListener(new View.OnClickListener() {

@Override public void onClick(View v) { // TODO Auto-generated method stub startActivityForResult(new Intent("com.sunil.rrfi.ACTIVITY2"), request_code); } }); }

public void onActivityResult(int requestCode, int resultCode, Intent data) { if(requestCode == request_code){ if(resultCode == RESULT_OK){ Toast.makeText(this, data.getData().toString(), Toast.LENGTH_SHORT).show(); } } } }

Activity2.java (File : Activity2.java) package com.sunil.rrfi; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class Activity2 extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout2); Button button = (Button) findViewById(R.id.btnOK); button.setOnClickListener(new View.OnClickListener() {

@Override public void onClick(View v) { // TODO Auto-generated method stub Intent data = new Intent(); EditText username = (EditText) findViewById(R.id.username); //Set the data to pass back data.setData(Uri.parse(username.getText().toString())); setResult(RESULT_OK, data);

//Close the activity finish(); } }); } }

AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.sunil.rrfi" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".Activity1" 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=".Activity2" android:label="@string/app_name" > <intent-filter> <action android:name="com.sunil.rrfi.ACTIVITY2" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> </application> </manifest>

As you can see, we use many new things in this examplestartActivityForResult() To Call an Activity & wait for result to be returned from it, we need to use the startActivityForResult() method, likestartActivityForResult(new Intent( "com.sunil.rrfi.ACTIVITY2"), request_code);

request code In addition to passing in an Intent object, we need to pass in request code as well. The request code is simply an integer value that identifies an activity we are calling. This is needed because when an activity returns a value, we must have a way to identify it. For example suppose we are calling multiple activities at the same time & some activities not return result immediately. When an activity returns, we need this request code to determine which activity is actually returned. Note:- If the request code is set to -1, then calling it using the startActivityForResult() method is equivalent to the startActivity() method. That is no result will be returned.

setData() In order for an activity to return a value to the calling activity, we use an Intent object to send data back via the setData() method

setResult()

The setResult() method sets a result code (either RESULT_OK or RESULT_CANCELLED) and the data (an Intent object) to be returned back to the calling activity.

onActivityResult() In the calling activity, we need to implement the onActivityResult() method, which is called whenever an activity returns.

getData() The returned result is passed by data argument; and we can obtain its details through the getData() method.

Here is the output of above example -

When you click on "Start Activity For Result" Button, the new activity (Activity2) will be launched.

After filling username you need to click "OK" button to return to previous Activity (Activity1). The username you just entered in the Activity2 will be passed back to Activity1 (See the Toast Notification).

That's it ! You can also download the source code of this tutorial-

You might also like