MIDlet Execution Environment and Lifecycle

All MIDlets are derived from the abstract base class javax.microedition.midlet.MIDlet, which contains methods that the MIDP platform calls to control the MIDlet’s lifecycle, as well as methods that the MIDlet itself can use to request a change in its state. A MIDlet must have a public default constructor (that is, a constructor that requires no arguments), which may be one supplied by the developer if there is any initialization to perform or, when there are no explicit constructors, the empty default constructor inserted by the Java compiler. This is what a skeleton MIDlet class might look like:

public class MyMIDlet extends MIDlet {

    // Optional constructor
    MyMIDlet( ) {
    }

    protected void startApp( ) throws MIDletStateChangedException {
    }

    protected void pauseApp( ) {
    }

    protected void destroyApp(boolean unconditional) 
                              throws MIDletStateChangedException {
    }
}

At any given time, a MIDlet is in one of three states: Paused, Active, or Destroyed. A state diagram that shows how these states are related and the legal state transitions is shown in Figure 3-4.

The lifecycle of a MIDlet

Figure 3-4. The lifecycle of a MIDlet

When a MIDlet is loaded, it is initially in the Paused state. The usual class and instance initialization is then performed -- that is, static initializers are called the first time the MIDlet class is loaded, all instance initializers are invoked when the ...

Get J2ME in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.