Wednesday, September 5, 2012

Sending Data from One Activity to Other in Android

Hi friends
In this post we will learn how to send data(integer,long,string etc) from one activity to other.

Note: I hope you  are familiar with using Buttons and EditTexts if not then go through this post  Using Button and EditText in Android.

Some times while working with android we need to send some data from one Activity to other.
Here we will create a project which will have two activities "FirstActivity" and "Second Activity".
We send Name and Phone Number from  FirstActivity to Second Activity and display the sent data.

Create a new project.

Donot forget to add SecondActivity in manifest.

Editing main.xml file

Edit your main.xml file add two  textviews, two edittexts, and  a button.
It should look like (you can just copy the following xml code and paste in main.xml file)


<LinearLayout xmlns:android=""

        android:text="Phone Number"


Now open the FirstActivity class and write following

public class FirstActivity extends Activity 

    public void onCreate(Bundle savedInstanceState)
    Button button=(Button)findViewById(;
    final EditText editTextName=(EditText)findViewById(;
    final EditText editTExtPhone=(EditText)findViewById(;
    // add button onClick Listener
    button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String name=editTextName.getText().toString();
long phone=Long.parseLong(editTExtPhone.getText().toString());
// create a new intent
Intent intent =new Intent(getApplicationContext(),SecondActivity.class);
// put the name and phone(to be sent to other activity) in intent
intent.putExtra("PERSON_NAME", name);
intent.putExtra("PHONENUMBER", phone);
// start the second activity


Edit your SecondActivity class

Point to Note(To Rememeber)

intent.putExtra("PERSON_NAME", name);
we have put the name in intent with "PERSON_NAME" tag, we will fetch name in second activity with same tag(see second activity).  similarly we can send and fetch any type of data. (Do not forget to declare this activity in manifest file)

public class SecondActivity extends Activity 

public void onCreate(Bundle savedInstanceState)
// extract the name and phone from intent
String name=getIntent().getStringExtra("PERSON_NAME");
long phoneNumber=getIntent().getLongExtra("PHONENUMBER", 0);
TextView tv=new TextView(this);
String str="NAME : "+name+"\nPhone Number : "+phoneNumber;

Now run the application, you will see the following

More Android Tutorials

Android : Introduction

       Eclipse Setup for Android Development

                     Configuring Eclipse for Android Development

          Begging With Android

                     Creating Your First Android Project
                     Understanding Android Manifest File of your android app

         Working With Layouts

                      Understanding Layouts in Android
                          Working with Linear Layout (With Example)
                                Nested Linear Layout (With Example)
                          Table Layout
                          Frame Layout(With Example)
                         Absolute Layout
                         Grid Layout


                     Activity In Android
                     Activity Life Cycle
                     Starting Activity For Result
                     Sending Data from One Activity to Other in Android
                     Returning Result from Activity

     Working With Views

                     Using Buttons and EditText in Android 
                     Using CheckBoxes in Android 
                     Using AutoCompleteTextView in Android
                     Grid View

     Dialogs In Android

                     Working With Alert Dialog
                     Adding Radio Buttons In Dialog
                     Adding Check Boxes In Dialog
                     Creating Customized Dialogs in Android
                    Adding EditText in Dialog

                   Creating Dialog To Collect User Input

                 DatePicker and TimePickerDialog

                              Using TimePickerDialog and DatePickerDialog In android

    Working With SMS

                  How to Send SMS in Android
                  How To Receive SMS
                  Accessing Inbox In Android


               Populating ListView With DataBase

      Menus In Android

                    Creating Option Menu
                    Creating Context Menu In Android


                    Using Telephony Manager In Android

     Working With Incoming Calls

                    How To Handle Incoming Calls in Android
                    How to Forward an Incoming Call In Android
                   CALL States In Android


                   Notifications In Android
                   How To Vibrate The Android Phone
                   Sending Email In Android
                  Opening a webpage In Browser
                   How to Access PhoneBook In Android
                   Prompt User Input with an AlertDialog

   Storage:  Storing Data In Android

               Shared Prefferences  In Android

                             SharedPreferences In Android

               Files: File Handling In Android

                              Reading and Writing files to Internal Stoarage
                              Reading and Writing files to SD Card 

                DataBase : Working With Database

                             Working With Database in Android
                             Creating Table In Android
                             Inserting, Deleting and Updating Records In Table in Android
                             How to Create DataBase in Android
                             Accessing Inbox In Android

     Animation In Android:

                  Animating A Button In Android


  1. Thank You, This helped a bunch.

  2. can u solve this problem sir...

    I have 2 activity and some edit text and listview ...can u send data second to first in listview like contactlist

  3. Thanks for concept clearing tutorial and examples.

  4. Nice sir,
    if we want to set data in second Activity in text view which is declared in activity2.xml file..
    then what we have to do.?

  5. Hello, I'm happy to see some great articles on your site. Would you like to come to my site later? My site also has posts, comments and communities similar to yours. Please visit and take a look 메이저놀이터

  6. The assignment submission period was over and I was nervous, safetoto and I am very happy to see your post just in time and it was a great help. Thank you ! Leave your blog address below. Please visit me anytime.

  7. There is no game camp that guarantees that. When do you give a lot? or when giving less But if ดูบอลออนไลน์

  8. The entrance to the pg slot is the entrance to the most popular PG camp slot games. pgslot

  9. even more in the present There is a situation of the Covid-19 epidemic causing many players. Can't travel to play slots at the casino online slots are very popular. pgslot เว็บตรง

  10. with changing wild patterns at the start of each spin One to five customers will order food at the bar. ข่าวบอลวันนี้

  11. For playing slots games at night is considered another option. that you can withdraw, ผลบอลสด

  12. Just apply for membership with us, including camp. Ready to service 24 hours a day. ดูบอลออนไลน์

  13. And you are considered very lucky to meet our JILI Slot game camp because it is a game camp that can make money easily overnight only. So you don't need to worry. pgslot

  14. n ready position muscles are contracted and ready for action. To move, muscles must be relaxed pgslot

  15. or newcomers can be a part of slots games that can be played and get real money, low budget, low capital, don't worry because slots pgslot

  16. Pay real money that is easy to use, slots, transfer through wallets, no minimums, come to everyone. pgslot

  17. betting method in order to increase the chances of winning a bet. for themselves by those ฟุตบอลโลก 2022

  18. transactions with our direct web slots by yourself. No need to talk to admin. and no longer have to ambslot

  19. Know how to plan investment bets from playing games, no matter what game it is, it will rely on the pgslot


  21. deposit and withdrawal service, latest update in 2023, all members on the website can AMBKING

  22. by comparing statistics and found that modifying the amount in playing slots will allow you to earn bonuses slotxo

  23. Before using any website service, try to check the payout schedule or payout rate. of each website well pgslot