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!


2 thoughts on “Intents-An Example(Part 2)

    Gautham said:
    April 27, 2013 at 2:11 pm

    In order for the line
    Intent intent = new Intent(HelloWorldActivity.this, SecondActivity.class);
    to work I found that I had to manually declare the second activity in the manifest.

      Nikhil Gupta responded:
      May 8, 2013 at 7:21 am

      That’s absolutely true. I think I have mentioned it in a post.

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s