Showing posts with label life cycle. Show all posts
Showing posts with label life cycle. Show all posts

Nov 9, 2009

ActivityMIDlet: A MIDlet++

Hi all,

In the post "Life Cycle Methods: Do you use them?", I wrote about MIDlet's life cycle methods. Also in this post, I quickly mentioned how interesting is the life cycle of an Activity in Android platform. I said that, because an Activity provides more life cycle methods, providing more control and possibilities for the developers. For instance, an Activity has 7 life cycle methods, against only 3 of a MIDlet. With numbers, nobody discusses, right? To understand more about an Activity's life cycle, click here.

Anyway, after having written that post, I decided to implement a custom MIDlet that provided as many life cycle methods as possible as an Android's Activity. Unfortunately, not all methods were possible to put in this class; onRestart() and onStop() do not fit for Java ME, since it does provide a state where an application is shut down and restart by own application manager. That's why I left it out.

On the other hand, I haved added onCreate() and onResume(). The first one works like a constructor and the second is called when the application returns to the foreground, after a certain period on pause. In addition, the method's invoking order is such an Activity.

Other interesting method that I have added is onLowMemory(). This notifies the application when it is running on low memory level, allowing the application to take any providence to avoid crashing. This memory level can be configured by setMinimumMemory(long) method. For further information about these new methods, check the Javadoc.

To download the ActivityMIDlet class, click here. I hope you guys enjoy it.

See you in the next post...

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.