03 July 2009

Android Activity Analysis

I have been looking at the details of the life cycle of an Android Activity. There are many web sites that discuss this and many examples. But none of the example addressed all the onXXX() methods of Activity. I also found problems with some of the examples. In one case, the GUI update thread could be left running after the Activity supposedly was stopped.

I was trying to determine when the GUI update thread should be created, stopped, paused, etc that initiated this effort. I have a game underway (doesn't everyone?) and figuring out all the Activity infrastructure was giving me fits. Each example did it in a different manner and while there may be no one correct manner most of them seemed a little off. Usually they seemed to omit some of the steps needed to pause, stop, or destroy everything properly.

A number of discussions presented the life cycle as a state machine, as an alternative to the Google flow chart (or here). [I don't mean to single out Eric Burke of StuffThatHappens by using his chart to illustrate my point. He has good Android material on his site.] Like the examples, they didn't seem as well thought-out as possible. For example, notice in Eric's state chart how many places onResume() appears. That indicates to me that the representation of the state machine is needs more work.

I offer the chart below as a simpler representation which eliminates the redundant calls to onResume() and other routines. This diagram is simplified by the introduction of the Pause state reached by onStart() and onPause(). There may be another state after onCreate() and onRestart() are called but my work so far does not show it as important.


From Mystic Lake Software

The file accompaning this page, TemplateSurfaceView.zip, contains a skeleton application for an Android Activity using a SurfaceView as the drawing surface. The application doesn't do anything except report current state to LogCat.

In the TemplateSurvaceView (TSV), the Activity onXXX() routines are mimicked with corresponding doXXX() methods in the TSV class.

doStart() - create thread
doPause - pause thread
doResume - resume thread
doStop() - kill thread

It becomes clear that the onXXX() routines are nicely symmetrical with each pair being able to setup and tear down the parts needed for the application. The one misleading, oddball is onRestart(). At first thought it might somehow pair with doStart(), but this is not the case. The doStart() method is paired with doStop(). The doRestart90 appears to be available to duplicate some of the work done by onCreate() that may have been undone during onStop().

Once I had the diagram figured out the requirements became apparent and the TSV code was generally straightforward. Not so obvious was how to handle the Thread since many of the obvious Thread methods that would be used are deprecated due to deadlock problems. I won't go into the details here because this is a Java issue, not specific to Android. But read the article Why Are Thread.stop, Thread.suspend, Thread.resume and Runtime.runFinalizersOnExit Deprecated? for more information.

2 comments:

  1. An interesting model, but I think there are two errors:

    1. In this model the following path is possible: ... -> onStart -> onStop -> ... . This is not possible in Android. This is also not provided by the lifecycle model given by Google. We tried out all possible triggers to get this combination, but it simply didn't work. Do you have an application example, with which you can trigger this call series? There is a place in the documentation of android, which describes that it should be possible. But to me this seems to be an error in the documentation, too.

    2. The transition from state Pause to state Destroyed is missing. When a process is running and gets killed (e.g. by the system), onPause() will definitely be called, but onStop() and onDestroy() not.

    Regards!

    ReplyDelete
  2. @Androminik

    1. As my description indicates, I introduced Pause to simplify understanding of the lifecycle. Whether onStart through Pause to onStop can actually occur is not important. What is importnat is that Pause is a state where the application is not running and should have taken steps to be in a safe condition.

    2. My model is an attempt to make sense of the 'normal' lifecycle. The possibility of a 'kill' occuring, whether in Pause or any other state, is not from that perspective 'normal'.

    It may be better to think of my model as explaining not the application lifecycle per se but the abstraction that a developer needs to follow to successfully implement an Android application.

    ReplyDelete

SRC2 - Explicit Steering - Wheel Speed

SRC2 Rover This fourth post about the  qualifying round of the NASA  Space Robotics Challenge - Phase 2  (SRC2) addresses t he speed of the ...