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 ActivityLog.java source code below:

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

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.content.Context;
import java.io.*;
public class ActivityLog extends Activity {
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      String msg = "";
      msg += "Activity created\n";
      msg += "----------------\n";
      TextView tv = new TextView(this);
      tv.setText(msg);
      setContentView(tv);

      logLine("Checkpoint #1 - onCreate() called");
   }
   public void onStart() { //working
      super.onStart();
      logLine("Checkpoint #2 - onStart() called");
   }
   public void onResume() {
      super.onResume();
      logLine("Checkpoint #3 - onResume() called");
   }
   public void onPause() {
      super.onPause();
      logLine("Checkpoint #4 - onPause() called");
   }
   public void onStop() {
      super.onStop();
      logLine("Checkpoint #5 - onStop() called");
   }
   public void onRestart() {
      super.onRestart();
      logLine("Checkpoint #6a - onRestart() called");
   }
   public void onDestroy() {
      super.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");
         out.close();
         fos.close();
      } catch (Exception e) {
         e.printStackTrace(System.err);
      }
   }
}

Some notes on the enhanced ActivityLog.java 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.

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

 AboutAndroid - Application to Retrieve System Information

android.app.Activity 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

 Archived Tutorials

 References

 Full Version in PDF/EPUB