Question

I have a j2me midlet running on a cell phone. The code works fine, but the issue that comes up is that the program seems to be running more than one instance of itself. I have code at the beginning of the application inside the appStart() method that runs twice when the application starts. During the lifetime of the program, the code can be seen running twice when text is written to the screen.

The code looks like this:

public MyClass()
{
    form = new Form("MyProgram");
    cmdClose = new Command("EXIT", Command.EXIT, 1);

    form.addCommand(cmdClose);
    form.setCommandListener(this);

    display = Display.getDisplay(this);
    display.setCurrent(form);
}

public void startApp()
{
    form.append("App starting\n");
    // Rest of program
}

I have no idea why the code is being called twice.

I'm coding on the i290.

Was it helpful?

Solution

This is definitely a JVM bug. startApp() should be called only once at startup and can't be called again until pauseApp() is called or you call notifyPaused() yourself.

What I suggest is the following code:

private boolean midletStarted = false;

public void startApp() {
    if (!midletStarted) {
        midletStarted = true;
        //Your code
    }
}

This way you can track midlet state changes. But in fact it is better that you don't use this method at all and use constructor instead.

Oh, by the way, I don't think that there are some multiple instances or something like that, this is merely a JVM error.

OTHER TIPS

Maybe you did something that made the runtime call pauseApp() and then when you set the focus to the app the runtime called startApp() again.

Put logging in pauseApp() and see what happens.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top