Advertisement
Tech

How-to Use Intents In The Google Android Programming Environment. Part I

If we are developing a complex application in Android with some Activities, for instance maybe we need to pass information from one Activity to another, we need to know how to pass information. Intents are the answer so let’s take a closer look at how they work.

By Jbeerdev
Desk Tech
Reading time 3 min read
Word count 499
Google android Mobile Guides
How-to Use Intents In The Google Android Programming Environment. Part I
Advertisement
Quick Take

If we are developing a complex application in Android with some Activities, for instance maybe we need to pass information from one Activity to another, we need to know how to pass information. Intents are the answer so let’s take a closer look at how they work.

On this page

Intents are used in many ways. But, what it is an Intent? If we go to the Google Android Developers Reference we can find this definition:

An intent is an abstract description of an operation to be performed

Advertisement

What operations are we talking about? As mentioned in this articles header we can use Intents to pass information from one activity to another, this is just one functionality, we bind these two Activities…but we can bind services with Activities too! With Intents we can even make a service to update information in an Activity. But, what other operations can we perform with Intents?

I gave a little introduction of intents in the How-to create an Address Book in the Google Android Programming Environment – Finishing the Application , we create intents to perform the “Call to” action or the “Send a SMS to” action. Just as easy as:

Advertisement

Intent i = new Intent();

i.setAction(Intent.ACTION_CALL);

Advertisement

i.setData(Uri.parse(“tel:” + phoneNumber));

startActivity(i);

Advertisement

for the “Call to” action.

Further in this article, we will see more examples of useful intents.

Advertisement

Binding Intents

I called them “binding intents” just to point that we are going to talk about Intents that binds Activities, so we can pass information from one place to another.

Lets think of these intents using two examples:

Advertisement

-Example A: We need to give some information to a new Activity we are going to create now. This information will be for this example a phone number. In your first Activity you click a contact from a list of contacts, you then want to open a new window with this contact information in a new and amazing User Interface.

In the ActivityA.java

Advertisement

Intent intent = new Intent(“name.of.the.ActivityB”);

intent.putExtra(“INFO_TO_PASS”, variableToPass);

Advertisement

We create a new Intent, using the name of the ActivityB we have set in the AndroidManifest.xml.

<activity android:name=".BrightHub.ActivityB">

Advertisement

<intent-filter>

<action android:name=“name.of.the.ActivityB” />

Advertisement

<category android:name=“android.intent.category.DEFAULT”/>

</intent-filter>

Advertisement

</activity>

Where in “name.of.the.ActivityB” you can set the value you want.

Advertisement

We put the info we want to send in the “putExtra” method, we set a name to remember “INFO_TO_PASS” in this example, and the concrete info we want in the ActivityB.

In the ActivityB.java

Advertisement

Bundle extras = getIntent().getExtras();

variableGet = extras.getInt(“INFO_TO_PASS”);

Here we create a new Bundle object to get the information from the Intent. We can use “getInt”, “getString”, “getByte” to get the information. This method is similar to the GET and POST methods when we work in web programming environment.

More in Part II

In our next article we will explain the second example of the Intent application, and we will have a look at very useful intents!! Stay tuned.

This post is part of the series: How-to develop Google Android Applications - Going further

Here more functionality and simple how-to guides to show you how to work with Google Android

  1. Dev Guide to Translating Apps in Android
  2. Guide to Intents in Android Development
  3. Guide to Intents for Android Developers. Part II
  4. How to Program the Google Android Camera to Take Pictures
  5. Android UI Java Programming Tutorial
Keep Exploring

More from Tech

Filed under
Google android Mobile
More topics
Guides
Advertisement