Wednesday, December 4, 2013

Getting results from child activity: Android tutorial

Program description:

This program demonstrates how to pass data/results from sub activity (child activity) to Parent activity by using startActivityForResult method.

This program contains two activities first and second. First activity will contain a button to start second activity through explicit intent.

Second activity contains 2 buttons. 

On clicking first button second activity will send result success (RESULT_OK) to first activity. 
On clicking second button second activity will send result failure (RESULT_CANCELLED) and it will also send the reason for failure as "network failure" in intent.

Major functions used in this application:
startActivityForResult( intent, request_code); //used in first activity to start second activity.
onActivityResult(...);//used in first activity to catch the results from second activity.
setResult(..);//used in second activity to pass results back to first activity.
finish();//used in second activity to kill second activity to pass data back.



First Activity                                      Second Activity

 


When user clicks success in second   When user clicks failure in second
 


First Activity 
package com.techpalle.b15_childresult;

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.Toast;

public class MainActivity extends Activity {
 //step 1: define req codes for child screens
 public static final int CHLD_REQ1 = 1;
 Button b; //for going to next screen
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  b = (Button) findViewById(R.id.button1);
  //step2 : write logic to start second activity for result
  b.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    Intent in = new Intent(getApplicationContext(), 
      SecondScreen.class);
    startActivityForResult(in, CHLD_REQ1);
   }
  });
 }
 //step 4: handling the results from the chld screens in parent activity
 @Override
 protected void onActivityResult(int requestCode, 
   int resultCode, Intent data) {
  if(requestCode == CHLD_REQ1){
   if(resultCode == RESULT_OK){
    Toast.makeText(getApplicationContext(), "child 1 success", 
      0).show();
   }else{
    Bundle b = data.getExtras();
    String reason = b.getString("reason");
    Toast.makeText(getApplicationContext(), 
      "child 1 fail bcoz.."+ reason, 
      0).show();
   }
  }
  super.onActivityResult(requestCode, resultCode, data);
 } 

}



xml layout file for First Activity 
File name: 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"  
   android:paddingBottom="@dimen/activity_vertical_margin"  
   android:paddingLeft="@dimen/activity_horizontal_margin"  
   android:paddingRight="@dimen/activity_horizontal_margin"  
   android:paddingTop="@dimen/activity_vertical_margin"  
   tools:context=".MainActivity" >  
   <TextView  
     android:id="@+id/textView1"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="@string/hello_world" />  
   <Button  
     android:id="@+id/button1"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_alignLeft="@+id/textView1"  
     android:layout_below="@+id/textView1"  
     android:layout_marginTop="23dp"  
     android:text="Go to Second Screen" />  
 </RelativeLayout>  

Second screen
package com.techpalle.b15_childresult;

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 SecondScreen extends Activity {
 Button s,f;
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.second);
     s = (Button) findViewById(R.id.button1);
     f = (Button) findViewById(R.id.button2);
     
     //step 3: set results and finish
     s.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    setResult(RESULT_OK);//for success
    finish();    
   }
  });
     f.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    //setResult(RESULT_CANCELED);//for failure
    Intent in = new Intent();
    in.putExtra("reason", "network failure");
    setResult(RESULT_CANCELED, in);
    finish();    
   }
  });
 }

}

xml layout file for Second Activity 
File name: second.xml
 <?xml version="1.0" encoding="utf-8"?>  
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent" >  
   <Button  
     android:id="@+id/button1"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_alignParentLeft="true"  
     android:layout_alignParentTop="true"  
     android:layout_marginLeft="79dp"  
     android:layout_marginTop="106dp"  
     android:text="Success" />  
   <Button  
     android:id="@+id/button2"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_alignLeft="@+id/button1"  
     android:layout_below="@+id/button1"  
     android:layout_marginTop="93dp"  
     android:text="Failure" />  
   <TextView  
     android:id="@+id/textView1"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_alignParentTop="true"  
     android:layout_centerHorizontal="true"  
     android:layout_marginTop="31dp"  
     android:text="Welcome to Second Screen"  
     android:textAppearance="?android:attr/textAppearanceLarge" />  
 </RelativeLayout>  

Tags : startActivityForResult, pass data between activities, RESULT_OK, RESULT_CANCELLED, onActivityResult, setResult, finish, android tutorial, android free examples download

No comments:

Post a Comment