Tuesday, December 21, 2010

Activities in Android, minimum requirements

Activities are by far the most tricky element to handle in Android. It is so because its life-cycle is big and therefore is quite detailed. You can read a complete documentation at http://developer.android.com/guide/topics/fundamentals.html#actlife.

The aim here is not to explain how the whole Activity life-cycle works, since it is already well written in the link above. This text will briefly present what one must do minimally in order to make an Activity work smoothly.

The first method which must be implemented is the onCreate(Bundle). The reason for that is simple: in this method everything regarding the starting of an Activity is to be implemented. For instance, all listeners should be added here (if not were already added in the layout file), the database adapter should be initialised here, all parameters should be assigned here, and so on. To sum up, this method is where you initialise and set up everything you need.

There are other two methods which are also necessary. The onPause() and onResume() methods.

The former is called every time an Activity leaves the main execution thread, something which could happen any time. This method is also the first one market as killable. It means that after its execution (and after the execution of every other which follows it), the application can be killed. Therefore, its implementation must persist all relevant data.

The latter (onResume()) is the first one called every time an Activity becomes visible. Therefore, here all persisted data should be retrieved and displayed.

You may have noticed that here things work differently from many other applications because no "Save" button is pressed in order to persist (save) data. This is how Android usually works and if you think about it, it is good it is designed this way. The reason is simple: it allows you to switch applications with fewer clicks, which is much desirable for mobile applications.

There is one more method which must be talked about, the onSaveInstanceState(Bundle). When Android runs pretty low on resources, it may kill your activity with no warning. Thus, you need a way to recover from that. The onSaveInstanceState(Bundle) method is guaranteed to be called before the activity is killed. Therefore here you must persist data in order to restore the activity to its previous state. We persist data here in the Bundle, passed as parameter. This is accessed in the onCreate(Bundle) method, when the Activity is called back.

Note that all the four mentioned methods act together. For instance, when at first you call an application, the onCreate(Bundle) method is called. After that, the onResume() method is called. After that, when you leave an Activity, onSaveInstanceState(Bundle) the method is called followed by the onPause() method.

Here is the whole cycle: when the onCreate(Bundle) is called, it should restore the state from the Bundle, in case there is any. The onResume() method is called after that which should reinstate all data, in case there is any. Now, when one leaves the application, the onSaveInstanceState(Bundle) method is called followed by the onPause() one. The former should persist data which will restore the Activity state back (dynamic data), and the latter ought to persist data (preferably in the database). Note that when the Activity is killed by the Android due to low resources, the onSaveInstanceState(Bundle) is sure to be called, thus all data necessary for reinstating the Activity must be persisted in the Bundle.

There is an excellent example of this implementation at http://developer.android.com/resources/tutorials/notepad/notepad-ex3.html. It exercises everything which was described here.

How to start with Android

This is the first post on this blog and I would like to start with a simple guideline on how to start with Android. Fortunately, the answer is simple, read the Google documentation.

For that I would recommend the official Android Documentation web site: http://developer.android.com/guide/index.html

There you will find everything you need. However, to make things easier, I will give a roadmap of what is more important to read in order to start developing.

In the Dev Code tab read the following chapters:
  1. What is Android
  2. Application Fundamentals
  3. User Interface
  4. Content Provider
  5. Security and Permissions
  6. The AndroidManifest.xml
I know it is a lot of stuff to read, but by doing so, you will learn the most important about Android.

I strongly advise you to go to the Resources tab as well and read the tutorial. Also do the examples together. It helps you apply the theory you learned very nicely.

Finally, the tool: I like very much MOTODEV Studio for Android. Although the name suggests the tool is specific for a certain brand, you can you use tool for free and it covers the whole Android development life cycle.  The link is http://developer.motorola.com/docstools/motodevstudio/

I believe this covers the basic for learning Android.