Wednesday, 23 January 2013

StartActiviyForResult in Android

Starting another activity doesn't have to be one-way . you can also start another activity and receive result back . To receive a result , call startActivityForResult() instead of startActivity().

Method syntax:
startActivityForResult(intent, requestCode);

Example:
Intent in = new Intent(this,TargetActivity.class);
startActivityForResult(in, 1);

For result , your app can call camera app and receive the captured image as a result.

-->The activity that responds must be designed to return  a result. When it does it sends the result as another Intent object. Your activity result receives it in the onActivityResult() call beck method.

onActivityResult() syntax:


protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
}

Start the Activity:
There is nothing special about the Intent object you use the when starting for a result, but you do need to pass an additional integer argument to the startActivityForResult()  methd.

The integer argument is a "request code" that identifies your request. When you receive the result Intent, the callback provides the same request code so that your app can properly identify the result and determine how to handle it.

Receive the Result:
When the user is done with the subsequent activity and returns, the system calls your activity's onAcitivyResult() method. This method includes three arguments:


protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
}


  1. The request code you passed to startActivityForResult().
  2. A result code specified by the second activity. This is either RESULT_OK if the operation was successful or RESULT_CANCELED if the user backed out or the operation failed some reason.
  3. An Intent that carries the result data.

Example Application





ScreenShots:







 activity_main.xml:

<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"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/chossenamebutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="42dp"
        android:text="ChooseName" />

    <Button
        android:id="@+id/phnumberbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/chossenamebutton"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="45dp"
        android:text="choose phnumber" />

</RelativeLayout>

names.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" >

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Android" />

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="iphone" />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="BlackBerry" />

        <RadioButton
            android:id="@+id/radio3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Symbian" />

        <RadioButton
            android:id="@+id/radio4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Bada" />
    </RadioGroup>

</LinearLayout>

phonenumbers.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" >
    
<RadioGroup
        android:id="@+id/phradioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="12345" />

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="12346" />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="12347" />

        <RadioButton
            android:id="@+id/radio3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="12348" />

        <RadioButton
            android:id="@+id/radio4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="12349" />
    </RadioGroup>
    
</LinearLayout>

MainActivity.java:

package com.ram.startactivityforresult;

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;

public class MainActivity extends Activity {
Button chooseNameBtn, choosePhnumberBtn;
String name, number;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

chooseNameBtn = (Button) findViewById(R.id.chossenamebutton);
choosePhnumberBtn = (Button) findViewById(R.id.phnumberbutton);

choosePhnumberBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent in = new Intent(getApplicationContext(), PhNumbers.class);
startActivityForResult(in, 2);
}
});

chooseNameBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent in = new Intent(getApplicationContext(), Names.class);

startActivityForResult(in, 1);
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub

if (requestCode == 1) {
if (resultCode == RESULT_OK) {
name = data.getStringExtra("myname");
chooseNameBtn.setText(name);
}
} else if (requestCode == 2) {
if (resultCode == RESULT_OK) {
number = data.getStringExtra("phnumber");
choosePhnumberBtn.setText(number);
}
}




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

}

Names.java:
package com.ram.startactivityforresult;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class Names extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

setContentView(R.layout.name);

final RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);

rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
int id = rg.getCheckedRadioButtonId();

RadioButton rb = (RadioButton) findViewById(id);

String name = rb.getText().toString();

Intent in = new Intent();

in.putExtra("myname", name);

setResult(RESULT_OK, in);

finish();

}
});
}
}

PhNumbers.java:
package com.ram.startactivityforresult;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class PhNumbers extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

setContentView(R.layout.phonenumbers);
final RadioGroup rg = (RadioGroup)findViewById(R.id.phradioGroup);
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
int id = rg.getCheckedRadioButtonId();
RadioButton rb = (RadioButton)findViewById(id);
String number = rb.getText().toString();
Intent in = new Intent();
in.putExtra("phnumber", number);
setResult(RESULT_OK, in);
finish();
}
});
}

}

AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ram.startactivityforresult"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.ram.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="Names"></activity>
        <activity android:name="PhNumbers"></activity>
    </application>

</manifest>