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 SecondActivity.java, 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 HelloWorldActivity.java 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 HelloWorldActivity.java. 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 SecondActivity.java 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!