Click here to Skip to main content
15,916,280 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a J2ME project in Eclipse with Nokia SDK plugin, in which I use a Canvas skin. The users will press a key to quit the application.
But I don't really know the way to close after key pressing event.
Help me, please!

One simple way (which may not be very elegant, depending on how you have written your midlet): When you create your canvas, do it something like this:

in your Midlet, pass a reference to the Midlet itself to your Canvas constructor:
MyCanvas theCanvas = new MyCanvas(this, ...);

in the Canvas constructor, save the Midlet reference:
class MyCanvas extends Canvas implements CommandListener {
  MyMidlet theMidlet;

  public MyCanvas(MyMidlet m, ...) {
    this.theMidlet = m;


Then, when you want to close the midlet from keyPressed in MyCanvas,
just call theMidlet.destroyApp()

Share this answer
In your keyPressed handler, just call destroyApp(true).
destroyApp is what the system calls if your midlet is terminated externally, and it should do any cleanup required and call notifyDestroyed().
Share this answer
You know, keyPressed í available just inside the Canvas class. But the destroyApp and notifyDestroyed are methods of MIDlet class.
How can we call destroyApp(true) in my keyPressed handler?
Please explain me more clearly.
Share this answer
Thank you a lot, Peter. Your way is really good to solve my problem.
Share this answer
Peter_in_2780 24-Aug-10 18:09pm    
If you like it, vote for my answer above, and ACCEPT it so your question shows up as answered.
thankx peter :D.. i got stuck with the same problem but now everything is working fine :D
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900