Android StartAcitivity for result Example

By | October 11, 2012
When using startActivity, your application won’t receive any notification when the newly launched  sub activity finishes.

If you use the startActivityForResult() method instead of startActivity  you can receive data from the newly launched subactivity .

startActivityForResult() method request code parameter.This value will later be used to uniquely identify subactivity that has returned result.

When your sub activity is ready to return, call setResult method before finish to return a result to the calling activity

The setResult method takes two parameters the result code and the result itself,represented as intent.

Once the Sub-Activity finished, the onActivityResult() method in the calling Activity will be called. Inside this method you can receive the data sent by subactivity.

 Lets see an example

1. XML Layout

   Create XML Files
       1)screen_1.xml
       2)screen_2.xml

  file:-screen_1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp"
>
<Button
android:id="@+id/go_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Go and Get" />
<TextView
android:id="@+id/got_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>

   file :-screen_2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp" >
<EditText
android:id="@+id/take_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="Enter your name"
>
<requestFocus />
</EditText>
<Button
android:id="@+id/return_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Return" />
</LinearLayout>

2. Activities

     Create Activity Classes
        1)MainActivity.java
        2)Next_Activity.java

   file:-MainActivity.java

package com.example.com.pavan.fetchresult;

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

public class MainActivity extends Activity {

Button b_go;
TextView tv_got;
static final int request = 1;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen_1);
b_go = (Button) findViewById(R.id.go_id);
tv_got = (TextView) findViewById(R.id.got_id);
b_go.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, Next_Activity.class);
startActivityForResult(i, request);
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case request: {
if (resultCode == Activity.RESULT_OK) {
String ret_value = data.getStringExtra("key");
tv_got.setText(ret_value);
}
}
break;
}
}
}

    file:-Next_Activity.java

package com.example.com.pavan.fetchresult;

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

public class Next_Activity extends Activity {
Button b_ret;
EditText et_give;
String value = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.screen_2);
b_ret = (Button) findViewById(R.id.return_id);
et_give = (EditText) findViewById(R.id.take_id);
b_ret.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
value = et_give.getText().toString();
Intent result = new Intent(Next_Activity.this,
MainActivity.class);
result.putExtra("key", value);
setResult(RESULT_OK, result);
finish();
}
});
}
}

3. RUN