Friday, September 3, 2010

Midlet Lifecycle

A MIDlet lifecycle have following steps...
  1. startApp()
  2. pauseApp()destroyApp()
  3. distroyApp()
By default MIDlet is in a paused states. When the application is executed by default startApp() method will called.Its like the main meathod is core Java.When we need to close the application the destroyApp() method will be called. But when your constructor is not null type then it will be executed firstly. The source code of life cycle execution is as follows:

import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;

public class MidletLifecycle extends MIDlet{
 
  
  private Display display;//Initializing the Display.ONly one Display for one application.

   private Form form; //Initializing the Form.Form is a sub-class of Display

  public MidletLifecycle(){
    System.out.println("MidletLifecycle constructor");
  }

//As we know the main method of a mobile app
  public void startApp(){
    form = new Form("Midlet Lifecycle");
    display = Display.getDisplay(this);
    String msg = "This is the Lifecycle of Midlet!";
    form.append(msg);//adding the message to form
    display.setCurrent(form);//adding the form to display
  }

  public void pauseApp(){
    System.out.println("You are in pauseApp()...");
  }

  public void destroyApp(boolean destroy){
    System.out.println("You are in destroyApp()...");
    notifyDestroyed();
  }
}



 OutOut
MidletLifecycle constructor     //appears when the app is running
You are in destroyApp()...     //appears after the app is closed
javacall_lifecycle_state_changed() lifecycle: event is JAVACALL_LIFECYCLE_MIDLET_SHUTDOWNstatus is JAVACALL_OK

credit to : roseindia.net

No comments:

Post a Comment