In this exercise we are going to create a new project and in MainActivity we will override all of the basic Lifecycle callback methods. The idea is for each of the basic callback method to display a message to the user that method is called so we can see the complete Activity-lifecycle. We will be using toast messages to display messages to the user, because we want to see what is going on in the app itself. Note that this exercise is not about creating an application whit an actual use or functionality, but it is just for showing different states of the activity, for you to get the idea on what is happening behind the curtains in the activity lifecycle.
What is an Activity?
Some theory first, let us answer the question what is an activity. The Activity is one of the four basic app components in Android, it is the entry point for interacting with the user. It represents a single screen with a user interface. An activity is the window in which an application draws its UI. This window can fill the screen, but it can also be smaller than the screen. Generally, one activity implements one screen in an app. For instance, one of an app’s activities may implement a Settings screen, while another activity implements a Select Mode screen.
Most applications today contain multiple screens, which means they are compiled from multiple activities. Typically, one activity in an app is specified as the main activity that is the first screen to appear when the user launches the app.
Each activity can then start another activity in order to perform different actions. For example, the main activity in a simple note taking app may provide the screen that shows all of the current notes. From there, the main activity might launch other activities that provide screens for tasks like taking a new note and opening individual notes.
Activity-lifecycle
During its lifetime, an activity goes through a number of states. We use a series of callback methods to handle transitions between states of the activities.
Here are the basic callback methods we use to handle transitions between activity states:
1) onCreate()
This callback is implemented automatically when you create a new android project and you select the empty activity template. This callback is called when the system creates your activity. Your implementation should initialize the essential components of your activity: For example, your app should create views and bind data to lists here. Most importantly, this is where you must call setContentView() to define the layout for the activity’s user interface. When onCreate() finishes, the next callback is always onStart().
2) onStart()
As onCreate() exits, the activity enters the Started state, and the activity becomes visible to the user. Here you will have the activity’s final preparations for coming to the foreground and becoming interactive with the user.
3) onResume()
The system invokes this callback just before the activity starts interacting with the user. At this point, the activity is at the top of the activity stack, and captures all user input. Most of an app’s core functionality is implemented in the onResume() method. The onPause() callback always follows onResume().
4) onPause()
The system calls onPause() when the activity loses focus and enters a Paused state. This state occurs when, for example, the user taps the Back or Recents button. When the system calls onPause() for your activity, it technically means your activity is still partially visible, but most often is an indication that the user is leaving the activity. And the activity will soon enter the Stopped or Resumed state. An activity in the Paused state may continue to update the UI if the user is expecting the UI to update. Examples of such an activity include one showing a navigation map screen or a media player playing. Even if such activities lose focus, the user expects their UI to continue updating.
You should not use onPause() to save application or user data, make network calls, or execute database transactions. Once onPause() finishes executing, the next callback is either onStop() or onResume(), depending on what happens after the activity enters the Paused state.
5) onStop()
The system calls onStop() when the activity is no longer visible to the user. This may happen because the activity is being destroyed, a new activity is starting, or an existing activity is entering a Resumed state and is covering the stopped activity. In all of these cases, the stopped activity is no longer visible at all. The next callback that the system calls is either onRestart(), if the activity is coming back to interact with the user, or by onDestroy() if this activity is completely terminating.
6) onRestart()
The system invokes this callback when an activity in the Stopped state is about to restart. onRestart() restores the state of the activity from the time that it was stopped. This callback is always followed by onStart().
7) onDestroy()
The system invokes this callback before an activity is destroyed. This callback is the final one that the activity receives. onDestroy() is usually implemented to ensure that all of an activity’s resources are released when the activity, or the process containing it, is destroyed.
The Exercise
First off all, we need to create a new project, so we go to the android studio and create a new project. I am going to name my project Activity Lifecycle Exercise. We don`t need to change anything else, just click next, select an Empty Activity and finish the creation of the project.
We are not going to create any widgets we will just implement the basic seven callback methods of the activity lifecycle. You notice that the onCreate() method is implemented by the android studio itself when the empty activity is created
1 2 3 4 |
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } |
So now we just need to add a toast message to show us when this method is called. The code for creating a toast message in Android looks like this:
1 2 3 4 |
Toast toast = Toast.makeText(getApplicationContext(), "onCreate method started", Toast.LENGTH_LONG); toast.show(); |
When we add a toast message to the method it will look like this:
1 2 3 4 5 6 7 8 |
protected void onCreate(Bundle savedInstanceState) { Toast toast = Toast.makeText(getApplicationContext(), "onCreate method started", Toast.LENGTH_LONG); toast.show(); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } |
Next to implement the other basic callbacks, there is an easier way. Instead of typing them all, in Android Studio there is a shortcut for generating methods that are commonly used. So if you press Ctrl + o in windows you will get a list from where you can select a method to override/implement. you should select and implement onStart(), onResume(), onPause(), onStop, onRestart() and onDestroy(). Then you should add the toast message code to each of this methods. It will look something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
@Override protected void onStart() { Toast toast = Toast.makeText(getApplicationContext(), "onStart method started", Toast.LENGTH_LONG); toast.show(); super.onStart(); } @Override protected void onResume() { Toast toast = Toast.makeText(getApplicationContext(), "onResume method called", Toast.LENGTH_LONG); toast.show(); super.onResume(); } @Override protected void onPause() { Toast toast = Toast.makeText(getApplicationContext(), "onPause method called", Toast.LENGTH_LONG); toast.show(); super.onPause(); } @Override protected void onStop() { Toast toast = Toast.makeText(getApplicationContext(), "onStop method called", Toast.LENGTH_LONG); toast.show(); super.onStop(); } @Override protected void onDestroy() { Toast toast = Toast.makeText(getApplicationContext(), "onDestroy method called", Toast.LENGTH_LONG); toast.show(); super.onDestroy(); } |
Now you should start the app to see what will happen and which method will be called. Turn the phone or emulator from portrait mode to landscape mode and see what happens. Which of the methods are called. Here are several screenshots on how it looks like when the methods are called:
For more information on Activities in Android, you should visit this page.
Bookmarked!, I really like your web site!