Pages - Menu

Thursday 25 October 2012

Passing data to an Activity

Here we are going to see how to invoke an activity by passing a data to it. Passing of data to an activity is by means of attaching the data to the intent object. The steps you have to follow for invoking an activity by sending a data to it are given bellow.

Step 1:
             Create the intent object.
             It is the same step that we  already see in the start activity and start activity for result.
             Example:
                              Intent i = new Intent("com.codeglympse.SecondActivity");

Step 2:
            Create an object of the Bundle class
            The Bundle class is the supporting class for passing data to an activity.   
            Example:
                              Bundle bundle = new Bundle(); 

Step 3:
             Adding data to bundle object using putString() method
             Adding the data to the bumdle object by using the method putString().
             Syntax:
             bundle_object.putString(key,value);
             Here key is used for identifying a specific data from a pair of data. you can get a brief idea about it
             from the example provide bellow in this topic.
             Example:
                          bundle.putString("name", "Enter your name");
Step 4:
             Attach the Bundle object to the intent by using the method putExtras().
             Example:
                          i.putExtras(bundle); 

        
             
Step 5:
            Now start the activity using either startActivity() or startActivityForResult() method.

Step 6:
In order to receive the data from the intent object the target activity (called activity) must contains the following code segments.
       
             (a)    Get back the bundle object using the getIntent() . getExtras();
                  Example:
                                 Bundle bundle =  getIntent().getExtras();
             (b)   Get the data from the bundle object using getString() method
                  Example:
                                 String name = bundle.getString("Name");  where "Name" is the key value.

Consider the following example for more details about the concept. Followings are the codes for the android project PassingData.

(1) PassingDataActivity.java

package com.codeglympse;
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.Toast;
public class PassingDataActivity extends Activity {
int request_code =1;
Button bn;
 /** Called when the activity is first created. */
  @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    bn = (Button) findViewById(R.id.button1);
    bn.setOnClickListener(new OnClickListener() {

  @Override
    public void onClick(View arg0)
   {  
   Intent i = new Intent("com.codeglympse.DataActivity");
   Bundle extras = new Bundle();
   extras.putString("name", "Enter your name here");
   i.putExtras(extras);
   startActivityForResult(i, 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();
   }
   }
   }
   }

(2) Code for the called activity (here it is DataActivity.java )

package com.codeglympse;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class DataActivity extends Activity{
Button bn;
EditText txt;
String defaultname;
@Override
protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.data_activity);
bn = (Button) findViewById(R.id.b2);
txt = (EditText) findViewById(R.id.editText1);
Bundle extras = getIntent().getExtras();
if(extras!=null)
{
defaultname=extras.getString("name");
}
txt.setHint(defaultname);
bn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent data = new Intent();
data.setData(Uri.parse(txt.getText().toString()));
setResult(RESULT_OK, data);
finish();
}
});
}
}
In the above example we use a method setHint ( ), which is same as setText () except that it is used for providing guidelines for the user. (It is appear in diminished text and when the user begin to fill the text area it will disappear)


Here is the output of the project







                         
                                 



                          
            
                     




No comments:

Post a Comment