Activity (Android)

Let’s Code!

Posted on Updated on

Hi all!

So, I am back again. This time, we will delve into the coding arena and try to understand the code that is automatically generated when you create a new project (Remember! when we had run the project in the post titled Let’s Begin! , an app was launched in the emulator in spite of the fact that we had not written any code ourselves).

How did that happen? We have already seen that the layout which is automatically created when we make a new project is exactly same to the screen of that app. What about the rest of the its functioning? Who tells Android to set the screen to that particular layout? Let’s see!

If you open the project, click on the package name (in our case, it was com.hello.world) and open the source file (which is HelloWorldActivity.java in our HelloWorld project discussed in previous posts), you will find a few lines of code. The code which does the task mentioned above. But, the question is How?

The first line of code is simply to tell the package name to which this class belongs to(Class? For now, it is sufficient to understand that it is the most basic component of a JAVA code, which encapsulates other functions and variables . We will look at them in a greater depth in the coming posts). It’s the same as selected while making the project. The second line of code simply imports the android classes that are already present in the Android framework and provide us the functions required in the code.

Now, you can see we have declared a class with an access modifier called public which poses no restriction on the access (For those who are unfamiliar with it, we will come to that later). The name of the class is same as the name of the java file. Now, the code says that this class extends an Activity. So, what is an Activity? An activity is the top level component ( a pre-built class) which controls what you see on the screen. It may either cover the entire screen or a part of it, but only one activity runs at a time in Android. So, even if the activity covers just a part of the screen, the remaining part will be inactive till the time that activity is running.

So, the third line of code basically declares a class with no access restrictions, which extends an Activity (because of which we call this class an Activity as well!) so that it can interact with the screen, which means that this class can use all the functions and variables provided by the Activity class.

The rest of the code simply defines a function which is automatically called when this activity is first created. We don’t have to remember the syntax as this function is automatically generated. There are two things happening in this function. We don’t need to discuss the first one.

What about the line setContentView (R.layout.main)? Can you guess what this line of code does?

In the next post, we will discuss this line of code and also, dive deeper into the coding world of Android App Development.

Till then, BYE!

Advertisements