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”);
taskText.append(extraData+”\n”);

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!

About these ads

4 thoughts on “Transfer of data using Intents (Part 2)

    Subhash said:
    July 29, 2013 at 8:07 pm

    next post?????

      Nikhil Gupta responded:
      December 18, 2013 at 5:02 pm

      Please refer to my post. Sorry for the delay!

    pradeep said:
    August 19, 2013 at 12:43 pm

    Hi Nikhil Gupta, Your blog is wonderful and is very useful. I want to give u a suggestion i.e. plz change the layout of ur blog so that the header will also scroll along with the remaining. It is very difficult to read in small screens as header is occupying most of the space. or else keep a small header. This is just a suggestion.

      Nikhil Gupta responded:
      December 18, 2013 at 4:48 pm

      Done! Really sorry for the late response!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s