Android Tutorials for Beginners Part-1: Activity Component
Recommended by 28 users
In our previous posts, we have explained how to create Android apps and games. However, those Android tutorials might appear advanced to a beginner with limited knowledge of the technology. Our series of Android Tutorials would help beginners get familiar with the basic concepts of Android. This Android tutorial discusses the activity component.
Android Tutorials for beginners: Activity
- Activity provides the user a screen/interface to interact with the application.
- An application typically has multiple activities. Each screen of an application is a different activity. You can understand this using the following image: The first Activity (screen) has a button, which when clicked leads the user to a second screen, which is the second activity.
Let us take the example of your very own Facebook app. The log in screen is the first activity you see when you launch your Facebook app. After signing in, the next activity encountered is the news feeds screen.
- All activities of an application are loosely interconnected: An activity is designed around a particular action the user takes while interacting with another activity. In addition, one activity can make way for several different activities depending upon the user’s action.
- Activities can have full screen or floating windows.
- Main Activity: Generally, one activity of an application is the main activity. This is the activity (screen) presented to the user when the application is launched for the first time.
Stages in Activity Life-cycle
To develop a successful application, it is important for the developer to map out the complete flow of activities based on the user’s action. This would ensure that the application behaves exactly as expected. Therefore, understanding the Activity Life-cycle is very important for developers.
Following are the different states in an Activity Life-cycle:
1) Starting State:
When an activity does not yet exist in memory, it is in the starting state.
2) Resumed/Running State:
An activity that is in the foreground is in the running state. Any activity that is currently on the screen and interacting with the user is the running activity at that particular point in time. It exists at the top of the activity stack.
3) Paused State:
When an activity is not in focus (i.e. not interacting with the user), but is still visible on the screen, it is in the paused state.
4) Stopped State:
An activity that is not visible on the screen, but exists in the memory is in the stopped state.
5) Destroyed State:
A destroyed activity results from the removal of an activity (that is no longer required) from the memory. Such removals generally occur, when the activity manager decides that there is no use for such activities anymore.
The activities go through their life-cycle using a stack mechanism. “Last in-first out” system operates in the Android Activity stack. Here’s a brief overview of how the Activity Back-stack works:
- Running Activity (Activity 1): An activity interacting with the user at a particular point of time is at the top of the stack.
- Activity 2 starts: When a new activity starts, the system stops the previous activity, and pushes it to a lower level in the back-stack. The new activity is now in focus, and would remain to be so, as long as the user interacts with it.
- Position of Activity 1 in the stack: The original activity (Activity1) would lie just below the (new) currently running activity (Activity 2).
- Activity 2 destroyed: As soon as the user’s work with the current activity (Activity 2) is over, and he presses the back button, the original activity (Activity 1) comes to the foreground (after the system destroys the running activity).
Whenever an activity goes through any change in its life cycle stages (for instance: When a running activity is paused), a callback method notifies it of this change. Whether the activity is being created, destroyed, or undergoing any other transition, there is a callback method to notify it. For each callback, there is a pre-defined behavior.
To ensure smooth transitions between different activities, the developer must implement callbacks beforehand. We shall discuss more on callbacks in subsequent Android tutorials. Stay put for that.
Got a question for us? Please mention it in the comments section and we will get back to you.