04. The “Activity” Class

One of the first things you may have noticed when you created your app is that there are an amazing amount of files and folders associated with it. To start with, we will focus on just four of those…the first needing explanation is “StartActivity.java“:

Compared to our previous Java program this probably looks like a completely different language. Almost every aspect of this file involves new Java elements but this project is still, in essence, just a Java program. Keep that in mind while we learn the new concepts and don’t let the new items daunt you!

“extends”
The Activity class is one of the fundamental classes required to create an Android Application. Notice that our “StartActivity” class “extends” the Activity class. This basically means “Activity” has already been created and we are building on top of it. So the “Activity” class has methods and member variables that are available to any class that extends it. Without a starting point it would be too difficult to create the user interface required for an Android application ourselves and so we simply build on top of a foundation that already provides a lot of that functionality.

@override
This is related to the “extends” keyword. The Activity class already has a method called “onCreate” but it needs to be “redefined” in order to suit our project. The Activity class has already taken care of making sure “onCreate” is the first method run when you start your application and so we simply put the code we wish to have run initially, inside it.

Bundle
A “Bundle” is simply a java class that can hold a variety of objects and variables and pass them around. It’s used to pass data between activities mostly and in this case it just holds the information about the current state of the program. We won’t actually be using this Bundle for anything useful but we may encounter it again later so it’s worth mentioning.

super
This simply refers to the parent class. Since we are extending a class, we can access that classes methods and variables via the keyword “super“. In this example we are simply running the “onCreate” method found in the “Activity” class, don’t worry about what actually happens when it is called, it’s not important at this stage of your Android journey just know what “super” means for now.

setContentView
While “onCreate” has done a lot of the groundwork for setting up an Android UI, it hasn’t actually defined what that interface will look like…that’s our job and that is what “setContentView” does. We define a user interface as a separate XML file and pass that file into this method. In some ways, this makes Android a little like HTML + CSS, the style or look of your Android application is defined inside an XML document while it’s function is dealt with elsewhere.

So with that it’s now time to understand XML and how to define our user interface. If you have any experience with web design then this next phase will be much easier.

Leave a Comment

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>