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 SecondActivity.java 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) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
 }

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 HelloWorldActivity.java (Default Activity of our app), we want to switch to SecondActivity.java and when we click the button on SecondActivity.java, we want to return to HelloWorldActivity.java.

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!

Advertisements

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 )

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 )

w

Connecting to %s