Intent (Android)

Transfer of data using Intents (Part 2)

Posted on Updated on

Hi everyone!

In spite of trying hard, I couldn’t prevent the delay. I am again sorry for that. Let’s move on. In the last post, I introduced the concept of transfer of data between activities. I also described the code for declaring an Intent which could help us in accomplishing the task.

Now, it’s time to look at the code of, the second activity which will help us in adding new tasks to the list. As mentioned earlier, this activity will have an EditText to allow the user to input the task name and a Button, which when clicked, will take the user back to and add the task to the List. The code for the click listener for this button looks as follows:

  1. String taskName = taskEdit.getText().toString();
  2. Intent intent = this.getIntent();
  3. intent.putExtra(“task”, taskName);
  4. setResult(RESULT_OK, intent);
  5. finish();

Here, taskEdit is an object of class EditText. The first line extracts the data input to the taskEdit, converts it into string and stores it in a variable. Second line is used to grab access to the intent which called this activity. The third line is the one which actually does the job of putting the data onto the intent. intent.putExtra function used in this line basically adds the information contained in the second parameter to the intent and the first parameter provides a way to access it. We will see the use of the first parameter in a greater detail later, when we will try to access this information in I hope that the fourth and fifth lines will be pretty easy to understand. If not, please refer to the last three posts on Intents.

The above code ensures that the clicking of the button takes us back to the initial activity with an intent which contains the name of the new task that is to be added to the list.

Clearly, the callback function described in Part 1 of this post will be used to access the information carried by the intent since this function will be automatically called when the control is given back to this activity via an intent. Straight away, let’s look at the code!

String extraData=data.getStringExtra(“task”);

I think it is self-explanatory. We are extracting the information from the variable data using the value of the first parameter of the function in Line 4 above, and saving it in a variable called extraData. The second line just appends this value to the list (referred by taskText).

In this way, we received the name of the task from a different activity and display it in our main activity. This provides a clean and user-friendly interface which is the basis of a useful app.

But here, we have not taken care of the situation when the user calls the intent to but wants to cancel it later. This is not perfect programming, though it can be dealt very easily. How?

In the next post, we will finish our discussion on intent and move on to explore some new concepts in Android App Development.

Till then, BYE!

Transfer of data using Intents (Part 1)

Posted on Updated on

Hi all!

Last time, we had looked at the most basic communication which can be achieved among activities. It allowed us to switch between activities back and forth, which is an important concept used in almost all the android apps these days.

Moving on, it’s time to look at the data transfer using Intents. Consider the case of a simple Task application, in which a To-do list is shown in one activity while another activity performs the task of adding new items to the list. So, what’s happening here?

Basically, we need to create a new task in the second Activity and somehow transfer it to the first activity so that it could add it in the existing list. Note that we are not using any database. If we do so which is done most of the times, this app will be useless in itself. But, I am still discussing this app because I feel that it’s the best in order to understand the concept of transfer of data which you may need in various other apps.

In this post, I will not go through the layout or the entire code of the app. I may go through it later. But, I hope that you will be able to do so after going through the previous posts. As a hint, we will be using a TextView (to display the list) and a Button while making the first activity, while the second Activity will have an EditText and a Button.

Assuming that we have an EditText in the second Activity and when the user presses enter, the string in the EditText is captured in a string variable called NewTask, we need to simply tranfer the contents of NewTask to the first activity.

To achieve this, we need to call the intent when the button in pressed in the first activity in such a way that the Android platform knows that some data will be coming back to this activity. Continuing with the app from the previous post by replacing the startActivity(intent); by

startActivityForResult(intent, 1);

as a parameter acts as a unique code used to distinguish data received by this intent from the data received by other intents if more intents are used. Using the above functin, we have been able to call the intent, but we have not yet accessed the data which comes back with this intent.

To achieve this, we need to use a callback function which will called automatically when the intent returns. Let’s look at the code for this function:

public void onActivityResult(int requestCode,int resultCode,Intent data)
          super.onActivityResult(requestCode, resultCode, data);
                      //Code to extract the required information from the variable data

In our case, requestCode is 1. resultCode is a variable which is set to value RESULT_OK if the intent was successfully handled. data is the variable which contains the data received from the other activity.

In the next post, we will look at the code to extract the information as well as the code for the second Activity which puts the information in the intent.

Till then, BYE!

Intents-An Example(Part 2)

Posted on Updated on

Hi all!

Finally, I got some time to continue with the example. I am really very sorry for the long delay. From now on, I will be posting every two to three days. So, keep looking!

For a flashback,  we discussed the use of intents in achieving communication among activities. We also saw the basic code of the two activities which will communicate with each other in the last post. Without wasting more time, let’s discuss the final codes for both of these activities.

Remember! I told you that we will be playing with the onClickListeners of both the buttons in the respective activities to accomplish the task.

Let’s start with the code for Remove the code for showing the toast and replace it with a function called finish(). Yes! That’s it.

On clicking the button on the second activity, we just need to finish this activity and move back to Since, we will be calling this activity only from (How to call from others? We’ll see!), finishing this activity will automatically take us where we want. Hence, the code for is over.

How about It’s the place where we will be actually calling the intent.

So, replace the code for the toast in the onClickListener with the following lines of code:

Intent intent = new Intent(HelloWorldActivity.this, SecondActivity.class);

I hope that you are able to understand it clearly. We are basically making an object of class Intent passing to it two parameters. The first parameter is a reference to the current activity. this is a keyword which always refers to the current object. Hence, HelloWorldActivity.this simply specifies the activity from which the intent is being called. The second parameter is the one which specifies the activity to which we want to move. Remember, we specify it as .class. Hence, we have specified the second parameter as SecondActivity.class. But, the first line declares an Intent object called intent. To actually accomplish the task,  we need to call a function called startActivity with the parameter as the Intent object, which transfers the control to the activity specified while declaring the Intent object.

So, just run the above application and check out what it does! You will see that you will be able to go back and forth every time you click the button on the screen.

But, there is no practical use of this communication. Intents become more meaningful when we need to transfer some data from one activity to another.

In the next post, we will be looking at an application which involves some useful transfer of data.

Till then, BYE!


Intents – An Example (Part 1)

Posted on Updated on

Hi everyone!

Sorry for the delay in the post this time! Let’s go ahead!

In the last post, I gave an overview of something called Intents in Android. If you have not gone through it, please refer to Communication between Activities using Intents! first and then, come back here!

This time, I am going to discuss the simplest app which can demonstrate the use of Intents. Even though, the app in itself is of no use but, concepts discussed during its development will help us in our future endeavors. I will be building on our previous HelloWorld App which (till now) simply shows a Toast when we touch a  button on the screen.

First of all, we will be needing two activities in our project which will be communicating with one another. So, simply go to the project folder in the HelloWorldApp that we have been developing upon, right click, select New and the, click on Class. Fill the fields as per the following figure and select Finish.

A new activity called has been created. But, you will see that there is no onCreate function. Just copy the code from below. It will be exactly the same as the one automatically generated when you create a new project and hence, you don’t need to remember it.

public void onCreate(Bundle savedInstanceState) {

Note the change in the value of the parameter of setContentView. Yes, it refer to a new layout file called second.xml. Simply make a new file called second.xml in the layout folder and copy the contents from main.xml to this file as we want the similar layout for this activity too. To create the new xml file, simply right click on the layout folder and select New->File. Name the file as second.xml and select Finish. Remember to give a different ID to the button of second.xml to avoid conflicts. Just change the value of android:id attribute of Button tag to something that was not used for main.xml. Let me change it to button2. Also, change the text of this button so that we could distinguish between the two activites.

Now, we need to play with the onClick Listeners of the two buttons (present in the two activities) to start the communication. In this app, we simply want to switch between the activites when the button is pressed. When we click the button on (Default Activity of our app), we want to switch to and when we click the button on, we want to return to

Try to search and write the code for the same yourself based on our discussion in the previous post. In the next post, I will discuss it and some more concepts in the Android app development.

Till then, BYE!

Communicating between activities using Intents!

Posted on Updated on

So, we have completed the development of our first app and it’s time to move on! This time, we will ponder upon the question, “How to communicate between activities?”, i.e., Is it possible to switch from one activity to another in an app and if so, how? This post will try to answer most of these questions?

Let’s start from the very basic question! Why do we need to switch between activities? In an app, we need to have multiple activities which perform various tasks. As an example, a Message application has at least an Activity for showing the entire list of threads and an activity for creating new messages. Hence, we need to switch between these activities over and over again in order to make a user-friendly Message application. Similar situation arises in most of the apps since most of them consist of more than one activity.

So, we can see that we need to switch between activities, and most of the apps consist of various activities which can switch between one another.  Now, we need to see how it is possible. Clearly, the title of the post suggest that there is something called Intents in an Android App which can help us in this regard.

Intent is the part of the Android framework which facilitates communication between activities. An intent can be treated as a function which can be called in an Activity with the parameters which (in broad sense) give the location of the activity to which we need to switch over.

One can use intents to not only switch between activities, but also to share some data from one activity to another. We will talk about it in detail later.

Once an intent is called, the control switches over to the other activity. The other activity after completing its job can simply call a function finish() in order to finish the activity and pass the control to the earlier activity.

When the control is sent back to the earlier activity, a specific function (called a callback function) is executed which can be used to process the data sent by the other activity (if any!).

So, we have discussed the complete flow of control with the use of Intents! In the next post, we will start the development of the second app based on intents where we will look at the exact code.

Till then, BYE!