Thursday, July 26, 2012

Application restarts after incoming call

J2ME/LWUIT application lifecycle causes the application to pause when there is an incoming call or when user minimizes the application. In that case pauseApp() is called on the Midlet. When the application is resumed again, it agains comes to the run() method. If your implementation doesn't handle the lifecycle correctly, it restarts after coming out of pause. The reason is that a normal run() implementation looks likes this-


    public void run() {
       new StateMachine("/myres.res");
    }

This causes a new StateMachine object being created when the app resumes. To over come the problem, use a member variable that holds the StateMachine object and modify the run() method as shown below-

    StateMachine mStateMc = null;



    public void run() {
        if (mStateMc == null) {
            mStateMc = new StateMachine("/myres.res");
        }
    }


This will resolve the app restart issue.