Class Lifecycle

java.lang.Object
com.codename1.system.Lifecycle

public class Lifecycle extends Object
Optional helper class that implements the Codename One lifecycle methods with reasonable default implementations to help keep sample code smaller.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    Callback that can be overriden to disable or modify crash protection
    void
    Callback when the app is destroyed
    protected Form
    Returns the form currently stored by this Lifecycle instance for application resume handling.
    protected int
    Returns the default number of network thread count
    The theme instance
    protected String
    Returns the name of the global theme file, by default it's "/theme".
    protected void
    Invoked on a network error callback
    void
    init(Object context)
    Invoked when the app is "cold launched", this acts like a constructor
    protected void
    onReady(boolean firstLaunch)
    Called once the application is on screen and #start() has returned: after the first launch and after every resume, as its own task on the EDT.
    void
    Invoked when the app is launched or resumed because the user shared content (text, a URL, a file or an image) into it from another application.
    protected void
    onResume(Form current)
    Called by #start() when the application returns to the foreground with a form to return to.
    void
    Shows the first form of the application.
    protected void
    The current form within the application lifecycle which possibly differs from the one in the implementation
    void
    Called by the OS on the first launch and every time the application comes back to the foreground: after stop(), when the user taps a notification, or when another application (a provisioning flow, a share sheet, a payment app) brings this one forward.
    void
    Callback when the app is suspended

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Lifecycle

      public Lifecycle()
  • Method Details

    • init

      public void init(Object context)

      Invoked when the app is "cold launched", this acts like a constructor

      Parameters
      • context: some OSs might pass a native object representing platform internal information
    • bindCrashProtection

      protected void bindCrashProtection()
      Callback that can be overriden to disable or modify crash protection
    • getNetworkThreadCount

      protected int getNetworkThreadCount()

      Returns the default number of network thread count

      Returns

      currently two threads

    • getThemeName

      protected String getThemeName()

      Returns the name of the global theme file, by default it's "/theme". Can be overriden by subclasses to load a different file name

      Returns

      "/theme"

    • getTheme

      public Resources getTheme()

      The theme instance

      Returns

      the theme

    • handleNetworkError

      protected void handleNetworkError(NetworkEvent err)

      Invoked on a network error callback

      Parameters
      • err: the network error event
    • start

      public void start()

      Called by the OS on the first launch and every time the application comes back to the foreground: after stop(), when the user taps a notification, or when another application (a provisioning flow, a share sheet, a payment app) brings this one forward.

      Shows the form the application was on through #onResume(Form) when there is one, and calls #runApp() otherwise. Either way it then schedules #onReady(boolean), which runs after this method has returned.

      Do not block here, or in runApp() or onResume(Form), which run inside it: no modal Dialog.show(), invokeAndBlock() or NetworkManager.addToQueueAndWait(). The OS expects this method to return, and a push can arrive while it is blocked. The simulator throws a com.codename1.ui.BlockingDisallowedException for a blocking call made here so the mistake is found at the line that makes it. Put such code in onReady(boolean).

    • runApp

      public void runApp()

      Shows the first form of the application. Called by #start() when there is no form to return to: on the first launch, and on a later start when stop() found none.

      It runs inside start(), so it must not block; see #start(). Build and show the form here, and do anything that waits -- a login check against the server, an SDK that shows its own dialog, a permission prompt -- in #onReady(boolean).

    • onResume

      protected void onResume(Form current)

      Called by #start() when the application returns to the foreground with a form to return to. The default shows current again. Override it to refresh what that form shows, or to route somewhere else, for example back to a login form after a timeout.

      It runs inside start(), so it must not block; see #start().

      Parameters
      • current: the form that was showing when the application was stopped
    • onReady

      protected void onReady(boolean firstLaunch)

      Called once the application is on screen and #start() has returned: after the first launch and after every resume, as its own task on the EDT.

      This is the place for startup work that waits or shows something on top of the form: validation dialogs, a backend or SDK call, re-authentication when the application returns. Blocking calls are allowed here. Pushes that arrive while such a call is in progress are held until it returns (see com.codename1.push.Push#setDeliveryDeferred(boolean)), so a push() callback does not run in the middle of it.

      The default does nothing.

      Parameters
      • firstLaunch: true the first time after the application process started, false on a resume
    • stop

      public void stop()
      Callback when the app is suspended
    • destroy

      public void destroy()
      Callback when the app is destroyed
    • onReceivedSharedContent

      public void onReceivedSharedContent(SharedContent content)

      Invoked when the app is launched or resumed because the user shared content (text, a URL, a file or an image) into it from another application. The default implementation does nothing; override it to handle the shared payload.

      This is delivered on the EDT. File and image items are exposed as com.codename1.io.FileSystemStorage paths.

      Parameters
      • content: the shared content
    • getCurrentForm

      protected Form getCurrentForm()

      Returns the form currently stored by this Lifecycle instance for application resume handling.

      In most application code, use CN.getCurrentForm() instead, as it returns the form that is actually displayed on the screen. This method is only useful when subclassing Lifecycle and you need to inspect or customize the form remembered by stop() and restored by start(), which may differ from the form currently displayed by the Codename One implementation.

      Returns

      the form stored in this lifecycle, or null if no form has been stored

    • setCurrentForm

      protected void setCurrentForm(Form current)

      The current form within the application lifecycle which possibly differs from the one in the implementation

      Parameters
      • current: the new current Form