Passing Data Between Activities

In this tutorial, we will type a text in an edittext on the first activity and pass it to the second activity.

  • Open your Android Studio and start a new project. If you forget how to open new project in android studio, follow this.

 

  • Our goal is passing a data from MainActivity to SecondActivity. Scenario is simple. An edittext (consider as editable textbox) and a button are placed at MainActivity screen. User type his/her name in edittext and click the button. Name string is passed to SecondActivity.

 

  • As always, we define all resources which are used by application.
    Open your res/values/strings.xml file and add below string items.
    As you see,the last item contains %1$s . That expression means that a string value will be placed instead of it at runtime.

    Open your res/values/colors.xml file and add below color items.

     
  • Lets design the layout of MainActivity screen. Open your res/layouts/activity_main.xml and write below.

     
  • Swicth to design mode and check how your layout looks.
    Screen Shot 2015-11-06 at 11.33.18

 

  • Enough with layout, we need add functionality to our edittext and buttons. Open your MainActivity and write below code.

     
  • So we need a SecondActivity. Click right on your package.Create a SecondActivity.java like below. (Images are taken from another tutorial)
    Screen Shot 2015-10-25 at 12.53.36Screen Shot 2015-10-25 at 12.53.50

 

  • Lets design the layout of SecondActivity screen. Right click on your res/layout folder and create a layout file named activity_second.
    Screen Shot 2015-10-25 at 12.58.18Screen Shot 2015-10-25 at 12.58.54

 

  • Open your res/layout/activity_second.xml file and write below code.

     
  • As you know, we need to declare SecondActivity to AndroidManifest.xml. So open your AndroidManifest.xml and add below activity tag.

     
  • Your AndroidManifest should looks like below. It doesn’t have to look exactly the same. Just look at where I declare the SecondActivity.

     
  • Open your SecondActivity and write below code.

     
  • Lets start SecondActivity from MainActivity. Open your MainActivity and change the body of onClick method like below. We have to some checking before starting second activity.

     
  • Now we have to get the name data in SecondActivity and set it to textview with ‘wellcome’ word. When you put the data into intent, what you use for <key> is important  because you have to get the data in SecondActivity with exactly the same <key>. In this scenario our key is “name”

    You can see the setText() method in above code.
    getResources().getString(<STRING_ID>,variable) is given as parameter. Remember, second_activity_wellcome_text is like below:

    Our string placeholder is %1$s.  What we give as variable placed on %1$s. You can examine developer.android.com for more.

     

  • Thats all. Run the application and see the result. See you next tutorials.
download_source_icon Download Source Code
If you don’t know how to open an existing Android Studio Project, follow this tutorial.

Bir Cevap Yazın