In this section, you are going to override lifecycle functions to eavesdrop on MainActivity’s lifecycle. Each implementation will simply log a message informing you that the function has been called. This will help you see how MainActivity’s state changes at runtime in relation to what the user is doing.
In Android, the android.util.Log class sends log messages to a shared system-level log. Log has several functions for logging messages. Here is the one that you will use most often in this book:
public static Int d(String tag, String msg)
The d stands for “debug” and refers to the level of the log message. (There is more about the Log levels in the section called For the More Curious: Log Levels near the end of this chapter.) The first parameter identifies the source of the message, and the second is the contents of the message.
The first string is typically a TAG constant with the class name as its value. This makes it easy to determine the source of a particular message.
Open MainActivity.kt and add a TAG constant to MainActivity:
Listing 3.1 Adding a TAG
constant (MainActivity.kt
)
import ... private const val TAG = "MainActivity" class MainActivity : AppCompatActivity() { ... }
Next, in onCreate(Bundle?), call Log.d(…) to log a message.
Listing 3.2 Adding a log statement to onCreate(Bundle?)
(MainActivity.kt
)
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) Log.d(TAG, "onCreate(Bundle?) called") setContentView(R.layout.activity_main) ... }
Now override five more lifecycle functions in MainActivity by adding the following after onCreate(Bundle?):
Listing 3.3 Overriding more lifecycle functions (MainActivity.kt
)
class MainActivity : AppCompatActivity() { ... override fun onCreate(savedInstanceState: Bundle?) { ... } override fun onStart() { super.onStart() Log.d(TAG, "onStart() called") } override fun onResume() { super.onResume() Log.d(TAG, "onResume() called") } override fun onPause() { super.onPause() Log.d(TAG, "onPause() called") } override fun onStop() { super.onStop() Log.d(TAG, "onStop() called") } override fun onDestroy() { super.onDestroy() Log.d(TAG, "onDestroy() called") } private fun updateQuestion() { ... } ... }
Notice that you call the superclass implementations before you log your messages. These superclass calls are required. Calling the superclass implementation should be the first line of each callback function override implementation.
You may have been wondering about the override
keyword.
This asks the compiler to ensure that the class actually has the function that you want to override.
For example, the compiler would be able to alert you to the following misspelled function name:
override fun onCreat(savedInstanceState: Bundle?) {
...
}
The parent AppCompatActivity class does not have an onCreat(Bundle?) function, so the compiler will complain. This way you can fix the typo now, rather than waiting until you run the app and see strange behavior to discover the error.
Run GeoQuiz and messages will start materializing in the Logcat tool window at the bottom of Android Studio, as shown in Figure 3.5. If Logcat did not open automatically when you ran GeoQuiz, you can open it by clicking the Logcat button at the bottom of the Android Studio window.
You will see your own messages along with some system output. To make your messages easier to find, you can filter the output using the value you set for the TAG constant. In Logcat, click the dropdown in the top right that reads Show only selected application. This is the filter dropdown, which is currently set to show messages from only your app.
In the filter dropdown, select Edit Filter Configuration to create a new filter. Name the filter MainActivity and enter MainActivity in the Log Tag field (Figure 3.6).
Click OK. Now, only messages tagged “MainActivity” will be visible in Logcat (Figure 3.7).
54.81.33.119