Download Code

Android Pass Data Activity Example

In android pass data activity example 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 studio pass data activity example Application Project.

After creating project go to your activity_main.xml .Drag and drop edit text and button.

activity_main.xml

Android Pass Data,android pass data activity,android pass data activity example,pass data activity example.

After that also create layout and an activity to receive contents from sender activity.No need to add something in second layout.You will just deal with second activity which will receive data.

Then go to your MainActivity.java class and do the following steps.

  • Create/declare edittext,button and refer their id’s.
  • Apply click event on button and get text from edittext.
  • When clicked initailize bundle and set data to bundle.
  • Then finally pass that data using intent.

MainActivity.java

package com.example.anonymous.studiopassdata;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity implements View.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 have passed data from first activity.It’s time to get data in another activity.So, go to your activity on my side it’s (dataReceiver) and do the following steps.

  • Use getIntent() method to get the intent you sent.
  • then getExtras() method to get extras data within the intent.
  • and finally get the string.

dataReceiver.java

package com.example.anonymous.studiopassdata;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

/**
 * Created by Anonymous on 9/19/2016.
 */
public class dataReceiver extends AppCompatActivity {

    @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();
    }

}

Note: Don’t forget to declare your new activity within Android Manifest.xml file.

Android Pass Data,android pass data activity,android pass data activity example,pass data activity example.

Done now run your android pass data activity example application project.

Android Pass Data,android pass data activity,android pass data activity example,pass data activity example.

That’s for this android pass data activity example tutorial.

Thanks.