Oct 5, 2009

Life Cycle Methods: Do you use them?

Hi all,

As soon as I decide to learn a new technology, I always start reading the basis. Some people prefer to go straight to the code, so they can see something running quickly. I believe by seeing a sample app running ASAP encourage people to continue on it. Start by just reading may be kind of boring in the beginning (sometime it really is!).

One of the things that I always pay much attention is on application life cycle topic. The ways a application behaves is very important for any good application design. If you do not consider it, you may have troubles soon. Life cycle method provides an important interface between the application and the platform's environment.

Many developers merely considers startApp() method as a counterpart of main(String[]). In fact, it is, but there are some particularities, e.g., the fact of startApp() can be called more than once during application life cycle. Sometimes, developers use this method as the MIDlet's constructor. That's wrong! If you do that, the code in this method can be executed again and again, for instance, when the application is resumed after receiving a call. Keep in mind that startApp() is intended to gather the necessary resources to get the application running, either by the very first start or resuming due an external event.

Other method from MIDlet's life cycle, which is usually forgotten, is pauseApp(). Developers forgot how scarce is the computational resources (e.g. memory) of a mobile device by ignoring this method. pauseApp() is intended to notify the app that it is about to be paused, so the platform can manage a device's external event, e.g., a incoming call. That's why the platform needs to pause the app so it can have all necessary resource to perform its task. However, it is expected that every MIDlet releases part of its resources by receiving a pauseApp() call. Otherwise, it can compromise the external event handling. By ignoring that, your MIDlet can even be destroyed, since the platform can force it to have all the resources it needs. So, try to use this method properly, it will help you a lot.

To conclude, the destroyApp(boolean) method is other one usually forgotten. This is one is very straightforward to explain: use it to clean up your house. Use this method to release all the resources your application is using before it is shut down. Doing that, you will avoid that any resource (e.g. data connection) remains open after the application is destroyed, what can degrade the device's performance. destroyApp() can also be used to conclude any running task or to garantee the integrity of any app's important data. For instance, check this article out, by Vikram Goyal, and see how destroyApp() can be very important to your application.

I believe that Java ME could have more life cycle methods, just like Android does. For instance, to distinguish startApp() calls. Currently, we need a flag to know if this method is being called for the first time or is being recalled after a pause. I wish I could have a resumeApp(), instead.

Who knows in MIDP 4, we will have this. :P

See you in the next post.

No comments: