Home E-Books J2ME Game Development: Issues and Troubleshooting destroyApp() Method

J2ME Game Development: Issues and Troubleshooting.(TM) - destroyApp() Method

by Massimo Perrone (Hanami Solutions)

3.4 destroyApp() Method.

Method destroyApp() will contain amongst others the following instructions:

public void destroyApp(boolean b)
{
            	...
		display.setCurrent(null);
                	IlCanvas=null;
                	display=null;
                	notifyDestroyed();

	  ...
	}

As you can see they are fairly simple to understand and do not much more than free system resources. Also in this case, this method will be invoked by mean of commandAction() that, to grasp a global picture of the subject, we will briefly examine.

All the softkeys instantiated in a MIDlet are successively handled by method commandAction().

Let's take the example of a softkey shutting down the MIDlet; we will create, whithin our main class, its accompanying command this way:

exitCommand = new Command("Quit", Command.EXIT,1);

later we will take care of show onscreen by using the related instruction:

addComand(exitCommand);

Now if the user will press this button we should make the application exit, how can we do it?

Inside the main class, whithin commandAction() method, we will have something very close to the following code:

        public void commandAction(Command c, Displayable s)
        {
			...
                	if (c == exitCommand)         
                	{
                        	try                        
                        	{
                                	destroyApp(false);
                        	}

	catch (Exception ex)
                        	{
                                	System.out.println("Unable to exit application\n"+ex);
                        	}
			}
			...
}

Simple, isn't?



Next Page >>