- Logging the Activity Lifecycle
- Rotation and the Activity Lifecycle
- Saving Data Across Rotation
- The Activity Lifecycle, Revisited
- For the More Curious: Testing onSaveInstanceState(Bundle)
- For the More Curious: Logging Levels and Methods
For the More Curious: Logging Levels and Methods
When you use the android.util.Log class to send log messages, you control not only the content of a message, but also a level that specifies how important the message is. Android supports five log levels, shown in Figure 3.16. Each level has a corresponding method in the Log class. Sending output to the log is as simple as calling the corresponding Log method.
Figure 3.16 Log levels and methods
In addition, each of the logging methods has two signatures: one which takes a tag string and a message string and a second that takes those two arguments plus an instance of Throwable, which makes it easy to log information about a particular exception that your application might throw. Listing 3.8 shows some sample log method signatures. Use regular Java string concatenation to assemble your message string, or String.format if you have fancier needs.
Listing 3.8 Different ways of logging in Android
// Log a message at "debug" log level Log.d(TAG, "Current question index: " + mCurrentIndex); Question question; try { question = mQuestionBank[mCurrentIndex]; } catch (ArrayIndexOutOfBoundsException ex) { // Log a message at "error" log level, along with an exception stack trace Log.e(TAG, "Index was out of bounds", ex); }