Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

MIDlet.destroyApp() vs MIDlet.notifyDestroyed()

 
Alibabra Sanjie
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the diff MIDlet.destroyApp() vs MIDlet.notifyDestroyed()? How come I called MIDlet.destroyApp() alone, but couldn't change the state to destroyed state, I have to called MIDlet.notifyDestroyed()? But AMS can call MIDlet.destroyApp() alone to change the state? When the AMS receives the MIDlet.notifyDestroyed(), and it decides to change the state to Destroyed state, how the AMS changge the state without calling MIDlet.destroyApp(), which supposed to be used by AMS to change state to destroyed state?
[ August 08, 2004: Message edited by: Alibabra Sanjie ]
 
Sathya Srinivasan
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The MIDlet.destroyApp() is something similar IN CONCEPT to a destructor in C++ or a finalize() in Java (although not the same).

This a general pattern used by many container-managed frameworks, where hook methods are provided to initialize and free resources required by the application.

The destroyApp() method is a place where the application can put some code to free up the resources it used. When the container (in this case, the Application Management Software or AMS) decides to destroy the MIDlet, it will call this method before destroying it.

The notifyDestroyed() method on the other hand, is a callback from the application to AMS to tell the AMS that it is ready to be destroyed. This is how the AMS will know to destroy the application if it wants to be destroyed voluntarily (like when user presses the Exit button).

An expanded lifecycle of a MIDlet is as follows:

1. AMS creates an instance of the MIDlet.
2. AMS calls the MIDlet.initApp() method.
3. User does his/her stuff with the application.
4. User gets bored or is done with the work and presses Exit button (or something similar).
5. Application calls the MIDlet.notifyDestroyed() method, indicating that it wants to be destroyed.
6. AMS gets the notification and calls the MIDlet.destroyApp() method.
7. AMS frees up reference to the application.
 
Alibabra Sanjie
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Example in a book Core J2ME
...

// Check to see if the Exit command was selected
public void commandAction(Command c, Displayable s)
{
if (c == cmExit) //yes, the Exit command was selected
{
try
{
// Is it ok to exit?
if (safeToExit == false) //if No
destroyApp(false); //why? just do nothing using ";" good enough
else
{
destroyApp(true);
notifyDestroyed();
}
}
catch (MIDletStateChangeException excep)
{
safeToExit = true; // Next time, let's exit
System.out.println(excep.getMessage());
System.out.println("Resuming the Active state");
}
}

the above code segment, I couldn't see the point to call: destroyApp(false); Just do nothing, the app wouldn't Exit, if you don't want the app terminate.

My code is equivalent like this: //it is simplier!
...
public void commandAction(Command c, Displayable s)
{
if (c == cmExit) //yes, the Exit command was selected
{
// Is it ok to exit?
if (safeToExit == true) //if No, do nothing
{
destroyApp(true);
notifyDestroyed();
}
else
{
safeToExit = true; // Next time, let's exit
}
}
}

Any problem with my code?
[ August 08, 2004: Message edited by: Alibabra Sanjie ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic