3.1 Activities
Last updated
Last updated
In the previous chapter, we mentioned how each activity consists of a layout file controlling the design and a class file to help with the logic behind all the different GUI elements. So far in our application we’ve only been working with one Activity
class, but an application can have several!
Let’s see an example of this in action with Eatery.
The two different screens displayed here are each their own activities in the codebase. When the user clicks an Eatery card in MainActivity
, we say we launch the second activity. In the onCreate()
method of the second activity, we populate the TextView
and wait times chart with the appropriate values. When the user clicks the back button in the second activity, then we say that the second activity has finished, and the display returns to MainActivity
(which is the last unfinished activity).
Returning to our project with the single MainActivity
class, we can see that MainActivity
extends AppCompatActivity
, which is a built-in class that extends android.app.activity
.
This tells us that MainActivity
is of type Activity
, and so all of the Activity
class methods become available for the programmer, including setContentView()
and the overridden method onCreate()
.