Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Android Application Development Cookbook

You're reading from   Android Application Development Cookbook Over 100 recipes to help you solve the most common problems faced by Android Developers today

Arrow left icon
Product type Paperback
Published in Mar 2016
Publisher
ISBN-13 9781785886195
Length 428 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Rick Boyer Rick Boyer
Author Profile Icon Rick Boyer
Rick Boyer
Kyle Mew Kyle Mew
Author Profile Icon Kyle Mew
Kyle Mew
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface 1. Activities FREE CHAPTER 2. Layouts 3. Views, Widgets, and Styles 4. Menus 5. Exploring Fragments, AppWidgets, and the System UI 6. Working with Data 7. Alerts and Notifications 8. Using the Touchscreen and Sensors 9. Graphics and Animation 10. A First Look at OpenGL ES 11. Multimedia 12. Telephony, Networks, and the Web 13. Getting Location and Using Geofencing 14. Getting your app ready for the Play Store 15. The Backend as a Service Options Index

Passing data to another activity

The intent object is defined as a messaging object. As a message object, its purpose is to communicate with other components of the application. In this recipe, we'll show you how to pass information with the intent and how to get it out again.

Getting ready

This recipe will pick up from where the previous one ended. We will call this project SendData.

How to do it...

Since this recipe is building on the previous recipe, most of the work is already done. We'll add an EditText element to the main activity so that we have something to send to SecondActivity. We'll use the (autogenerated) TextView view to display the message. Here are the complete steps:

  1. Open activity_main.xml, remove the existing <TextView> element, and add the following <EditText> element:
    <EditText
        android:id="@+id/editTextData"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    The <Button> element that we created in the previous recipe doesn't change.

  2. Now, open the MainActivity.java file and change the onClickSwitchActivity() method as follows:
    public void onClickSwitchActivity(View view) {
        EditText editText = (EditText)findViewById(R.id.editTextData);
        String text = editText.getText().toString();
        Intent intent = new Intent(this, SecondActivity.class);
        intent.putExtra(Intent.EXTRA_TEXT,text);
        startActivity(intent);
    }
  3. Next, open the activity_second.xml file and modify the<TextView> element to include the ID attribute:
    <TextView
        android:id="@+id/textViewText"
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
  4. The last change is to edit the second activity to look for this new data and display it on the screen. Open SecondActivity.java and edit onCreate() as follows:
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        TextView textView = (TextView)findViewById(R.id.textViewText);
        if (getIntent()!=null && getIntent().hasExtra(Intent.EXTRA_TEXT)) {
            textView.setText(getIntent().getStringExtra(Intent.EXTRA_TEXT));
        }
    }
  5. Now run the project. Type some text on the main activity and press Launch Second Activity to see it send the data.

How it works...

As expected, the intent object is doing all the work. We created an intent just as in the previous recipe and then added some extra data. Did you notice the putExtra() method call? In our example, we used the already defined Intent.EXTRA_TEXT as the identifier, but we didn't have to. We can use any key we want (you've seen this concept before if you're familiar with name/value pairs).

The key point about using name/value pairs is that you have to use the same name to get the data back out. That's why we used the same key identifier when we read the extra data with getStringExtra().

The second activity was launched with the intent that we created, so it's simply a matter of getting the intent and checking for the data sent along with it. We do this in onCreate():

textView.setText(getIntent().getStringExtra(Intent.EXTRA_TEXT));

There's more...

We aren't limited to just sending String data. The intent object is very flexible and already supports basic data types. Go back to Android Studio and click on the putExtra method. Then hit Ctrl and the Spacebar. Android Studio will bring up the autocomplete list so that you can see the different data types that you can store.

You have been reading a chapter from
Android Application Development Cookbook - Second Edition
Published in: Mar 2016
Publisher:
ISBN-13: 9781785886195
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image