Implementing Activity Callback Methods

This section provides a tutorial example on how to implement activity callback methods using ActivityLog application as an example to log a short message from each callback method in a log file.

In this tutorial, I am going to enhance my ActivityLog application to implement other callback methods. See the source code below:

 * Version 2.0 - Adding more callback methods
 * Copyright (c) 2015,, All Rights Reserved.
package com.herongyang.activity;

import android.os.Bundle;
import android.widget.TextView;
import android.content.Context;
public class ActivityLog extends Activity {
   public void onCreate(Bundle savedInstanceState) {
      String msg = "";
      msg += "Activity created\n";
      msg += "----------------\n";
      TextView tv = new TextView(this);

      logLine("Checkpoint #1 - onCreate() called");
   public void onStart() { //working
      logLine("Checkpoint #2 - onStart() called");
   public void onResume() {
      logLine("Checkpoint #3 - onResume() called");
   public void onPause() {
      logLine("Checkpoint #4 - onPause() called");
   public void onStop() {
      logLine("Checkpoint #5 - onStop() called");
   public void onRestart() {
      logLine("Checkpoint #6a - onRestart() called");
   public void onDestroy() {
      logLine("Checkpoint #6b - onDestroyed() called");
   public void logLine(String msg) {
      try {
         FileOutputStream fos = 
            openFileOutput("Activity.log", Context.MODE_APPEND);
         OutputStreamWriter out = new OutputStreamWriter(fos);
         out.write((new java.util.Date()).toString()+": "+msg+"\n");
      } catch (Exception e) {

Some notes on the enhanced code:

Now build and install the enhanced version of ActivityLog to the emulator. Then follow next tutorials on how to test the activity lifecycle by running ActivityLog in different ways.

Last update: 2015.

Table of Contents

 About This Book

 Installing JDK 1.8 on Windows System

 Installation of Android SDK R24 and Emulator

 Installing Apache Ant 1.9 on Windows System

 Developing First Android Application - HelloAndroid

 Android Application Package (APK) Files

 Android Debug Bridge (adb) Tool

 Android File Systems

 Android 4.0.3 File Systems

 AboutAndroid - Application to Retrieve System Information Class and Activity Lifecycle

 Introduction of Activity Lifecycle

 onCreate() and Other Callback Methods

 ActivityLog - Application to Create Log File

 Viewing Activity Log File with "cat" Command in "adb shell"

Implementing Activity Callback Methods

 ActivityLog Test - Activity Terminated by User

 ActivityLog Test - Activity Stopped and Restarted

 ActivityLog Test - Activity Paused and Resumed

 View Objects and Layout Resource Files

 Using "adb logcat" Command for Debugging

 Build Process and Package File Content

 Building Your Own Web Browser

 Android Command Line Shell

 Samsung Galaxy Tab 3 Mini Tablet

 USB Debugging Applications on Samsung Tablet

 Android Tablet - LG-V905R

 USB Debugging Applications on LG-V905R Tablet

 Android Phone - LG-P925g

 USB Debugging Applications on LG-P925g Phone

 Outdated Tutorials


 PDF Printing Version