Tuesday, 27 October 2015

StartActivityForResult 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 orRESULT_CANCELED if the user backed out or the operation failed some reason.
3.                 An Intent that carries the result data.

 activity_main.xml
-------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
   
xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
xmlns:app="http://schemas.android.com/apk/res-auto"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
app:layout_behavior="@string/appbar_scrolling_view_behavior"
   
tools:showIn="@layout/activity_main" tools:context=".MainActivity"
   
android:orientation="vertical">

    <
Button
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Choose Names"
       
android:id="@+id/button"
       
android:layout_gravity="center_horizontal" />

    <
Button
       
android:layout_width="wrap_content"
        
android:layout_height="wrap_content"
       
android:text="Choose Ph Number"
       
android:id="@+id/button2"
       
android:layout_gravity="center_horizontal"
       
android:layout_marginTop="25dp" />
</
LinearLayout>
MainActivity.Java
-----------------------------------
package com.android.anil.startactivityforresult;



import android.content.Intent;

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.support.v7.widget.Toolbar;

import android.view.View;

import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    Button namesbtn,numbtn;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

        setSupportActionBar(toolbar);

        namesbtn=(Button)findViewById(R.id.button);

        numbtn=(Button)findViewById(R.id.button2);

        namesbtn.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                Intent intent=new Intent(getApplicationContext(),NamesActivity.class);

                startActivityForResult(intent,1);

            }

        });

        numbtn.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                Intent intent1=new Intent(getApplicationContext(),NumbersActivity.class);

                startActivityForResult(intent1,2);

            }

        });

    }



    @Override

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if(requestCode==1){

            if(resultCode==RESULT_OK){

                String res1=data.getStringExtra("name1");

                namesbtn.setText(res1);

            }

        }else if(requestCode==2){

            if(resultCode==RESULT_OK){

                String res2=data.getStringExtra("num1");

                numbtn.setText(res2);

            }

        }

        super.onActivityResult(requestCode, resultCode, data);

    }

}

activity_names.xml
--------------------------
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     xmlns:app="http://schemas.android.com/apk/res-auto"     android:layout_width="match_parent"     android:layout_height="match_parent"     app:layout_behavior="@string/appbar_scrolling_view_behavior"     tools:showIn="@layout/activity_names"     tools:context="com.android.anil.startactivityforresult.NamesActivity"     android:orientation="vertical">     <RadioGroup         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_marginTop="20dp"         android:id="@+id/radiogroup1"         android:layout_gravity="center">     <RadioButton         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Android"         android:id="@+id/radioButton"         android:checked="false" />     <RadioButton         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Ios"         android:id="@+id/radioButton2"         android:checked="false" />     <RadioButton         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Windows"         android:id="@+id/radioButton3"         android:checked="false" />     <RadioButton         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Blackberry"         android:id="@+id/radioButton4"         android:checked="false" />     <RadioButton         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Symbain"         android:id="@+id/radioButton5"         android:checked="false" />     <RadioButton         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Bada"         android:id="@+id/radioButton6"         android:checked="false" />     </RadioGroup> </LinearLayout>
NamesActivity.Java
------------------------
package com.android.anil.startactivityforresult; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.widget.RadioButton; import android.widget.RadioGroup; public class NamesActivity extends AppCompatActivity {     RadioGroup rg1;     RadioButton rb1;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_names);         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);         setSupportActionBar(toolbar);         rg1=(RadioGroup)findViewById(R.id.radiogroup1);         rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {             @Override             public void onCheckedChanged(RadioGroup group, int checkedId) {                 int id=rg1.getCheckedRadioButtonId();                 rb1=(RadioButton)findViewById(id);                 String name=rb1.getText().toString();                 Intent in1=new Intent();                 in1.putExtra("name1",name);                 setResult(RESULT_OK,in1);                 finish();             }         });     } }
activity_numbers.xml
------------------------------
<?xml version="1.0" encoding="utf-8"?> <LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     xmlns:app="http://schemas.android.com/apk/res-auto"     android:layout_width="match_parent"     android:layout_height="match_parent"     app:layout_behavior="@string/appbar_scrolling_view_behavior"     tools:showIn="@layout/activity_numbers"     tools:context="com.android.anil.startactivityforresult.NumbersActivity"     android:orientation="vertical">     <RadioGroup         android:id="@+id/radiogroup2"         android:layout_width="wrap_content"         android:layout_height="wrap_content"        android:layout_gravity="center"         android:layout_marginTop="25dp">     <RadioButton         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="0123456"         android:id="@+id/radioButton7"         android:checked="false" />     <RadioButton         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="0123457"         android:id="@+id/radioButton8"         android:checked="false" />     <RadioButton         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="0123458"         android:id="@+id/radioButton9"         android:checked="false" />     <RadioButton         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="0123459"         android:id="@+id/radioButton10"         android:checked="false" />     <RadioButton         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="0123460"         android:id="@+id/radioButton11"         android:checked="false" />     <RadioButton         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="0123461"         android:id="@+id/radioButton12"         android:checked="false" />     </RadioGroup> </LinearLayout>
NumbersActivity.Java
---------------------------------
package com.android.anil.startactivityforresult; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.widget.RadioButton; import android.widget.RadioGroup; public class NumbersActivity extends AppCompatActivity { RadioGroup rg2;     RadioButton rb2;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_numbers);         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);         setSupportActionBar(toolbar);         rg2=(RadioGroup)findViewById(R.id.radiogroup2);         rg2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {             @Override             public void onCheckedChanged(RadioGroup group, int checkedId) {                 int id = rg2.getCheckedRadioButtonId();                 rb2 = (RadioButton) findViewById(id);                 String num = rb2.getText().toString();                 Intent in1 = new Intent();                 in1.putExtra("num1", num);                 setResult(RESULT_OK, in1);                 finish();             }         });     } }
Output is:
-------------------------------------------








No comments:

Post a Comment