Download Code

In this tutorial you will learn how to pass_data between activities in android application.Let me give some introduction about bundle and how you will pass_data between activities.

Bundle

Bundle is basically used to convert string into a form which can be parced.There are a lot of public and inherited methods of bundle but in this tutorial you will use putString() which contains key value pair.

You will also use Intent to start/pass_data to new activity and also put key using putExtra() method with that Intent.If you are not familiar with how to start an activity go to that tutorial first.

You will use Edit Text to get user input using first activity,then pass that user input to second activity,summed up and display in a Toast.

Pass_Data or Passing of Data

Alright let’s start go ahead and create Android Application Project.

Pass data,Pass_data,how to pass data between activities in android,

After creating project go to your activity_main.xml .Drag and drop edit text and button.Both layout will look like this.

Graphical Layout

Pass data,Pass_data,how to pass data between activities in android,

XML Layout

Pass data,Pass_data,how to pass data between activities in android,

Now go to your MainActivity.java class create variables for edit text,button,a private method to refer the id’s of UI components and call that method in onCreate() method.Then your MainActivity will look like this.

public class MainActivity extends Activity {

	EditText userInput; // edit text variable
	Button next;        // button variable to pass data to next activity
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initialize(); // calling id refering method
    }
    // method to refer all id's of all UI components
    private void initialize(){
    	userInput = (EditText) findViewById(R.id.editText1);   // refering edittext
    	next =(Button) findViewById(R.id.button1);             // refering  button
    }

}

Now you will apply click event on button.From now onward you will implement classes for better programming.In your MainActivity.java class implements onClickListener just like this.

public class MainActivity extends Activity implements OnClickListener{
}

Remember one thing when you write this at the same MainActivity will underline with red color.Hover the cursor over the MainActivity and add unimplemented methods.Call click event by passing this pointer to that click view.Then your MainActivity will look like this.

package androidcodec.data;

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;


public class MainActivity extends Activity implements OnClickListener{

	EditText userInput; // edit text variable
	Button next;        // button variable to pass data to next activity
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initialize(); // calling id refering method
    }
    // method to refer all id's of all UI components
    private void initialize(){
    	userInput = (EditText) findViewById(R.id.editText1);   // refering edittext
    	next =(Button) findViewById(R.id.button1);             // refering  button
    	next.setOnClickListener(this);     // calling click event 
    }
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}

}

Next you will get id of any clickable UI component within onClick method and put switch and case in that method.Just like this.

public class MainActivity extends Activity implements OnClickListener{

	EditText userInput; // edit text variable
	Button next;        // button variable to pass data to next activity
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initialize(); // calling id refering method
    }
    // method to refer all id's of all UI components
    private void initialize(){
    	userInput = (EditText) findViewById(R.id.editText1);   // refering edittext
    	next =(Button) findViewById(R.id.button1);             // refering  button
    	next.setOnClickListener(this);     // calling click event 
    }
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int id = v.getId(); //getting id
		switch(id)
		{
		case R.id.button1:
			
			break;
		}
	}

}

Now first create new activity named it as dataReceiver and also create new XML Layout.Then you activity after extending from Activity class will look like this.

public class dataReceiver extends Activity {

}

Now next and important step is to declare that new activity in Android Manifest file like this.
Pass data,Pass_data,how to pass data between activities in android,
Now go to your MainActivity here you will get user input from edit text using gettext() method.Then create bundle variable and put key value pair using putString() method.Now start new activity using intent and pass that bundle data within that intent using putExtra() method.Final MainActivity will look like this.

MainActivity.java

package androidcodec.data;

/*
 * 
 * All Rights Reserved By:Farhan Rasheed
 * 
 * */

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;


public class MainActivity extends Activity implements OnClickListener{

	EditText userInput; // edit text variable
	Button next;        // button variable to pass data to next activity
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initialize(); // calling id refering method
    }
    // method to refer all id's of all UI components
    private void initialize(){
    	userInput = (EditText) findViewById(R.id.editText1);   // refering edittext
    	next =(Button) findViewById(R.id.button1);             // refering  button
    	next.setOnClickListener(this);     // calling click event 
    }
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int id = v.getId(); //getting id
		switch(id)
		{
		case R.id.button1:
			String data = userInput.getText().toString();    // getting user input and store in data variable
			Bundle b = new Bundle();                         // creating new bundle variable
			b.putString("passing", data);                    // putting key value pair
			Intent pass = new Intent(getApplication(),dataReceiver.class);     // intent to start new activity
			pass.putExtras(b);                                                //putting bundle within intent
			startActivity(pass);                                              // starting activity by passing intent
			break;
		}
	}

}

Now you are done with MainActivity now to go new activity and create onCreate() method.In that use setContentView() method to set XML Layout.Next get value from intent and display in a Toast.Your new activity will look like this.

package androidcodec.data;

/*
 * 
 * All Rights Reserved By:Farhan Rasheed
 * 
 * */

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class dataReceiver extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.data);
		
		/*
		 * First getting intent then go inside intent putExtra method and finally going inside bundle and retreive value of passing
		 * 
		 * */
		
		String r = getIntent().getExtras().getString("passing");  
		Toast.makeText(getApplicationContext(), r, Toast.LENGTH_SHORT).show();
	}

}

That’s it you are done with this tutorial.I hope you get every step clearly,how to pass_data.If you want to test it by yourself you can download code and test.
Thanks for visiting don’t forget to leave your feedback.