Engineering books recommended by Raman

Tuesday, June 24, 2008

Android SDK

Not too long I started to poke around Android SDK. If you are like me and have been curious about the Android – software platform for mobile phones developed by Google, you must have gone over the sample apps, documentation and tutorials. These are pretty good and get one up and about the developing apps pretty soon.

Some of finer things that I noticed I will share with you here. Hopefully these will save you some time and help gain some insights as I did.

In the Notepad tutorial you will learn about the lifecycle of an Activity by the time you get to the Exercise 3. The apps you develop for mobile phone have to cooperate with the OS a lot more than what you may be used to and rightly so because of the constraints due to device’s limitations around power, memory limited screen estate and of course a lot more is going on there – calls and messages coming in while user playing music or watching videos; location alerts from GPS while driving – you get the point. So, as illustrated in the example, your app should be willing and ready to drop everything at anytime and then pick up later from where it was asked to let go. The tutorial explains various states (create, freeze, pause resume etc.) and the hooks provided to developers to take appropriate actions in the moments of app getting in those states. You will also read about how, to make user experience better, the code should be quick in letting go (you don’t want the user from answering the phone because your app is serializing stuff). One of the advices given is to close the database handlers and set them to null so that they can be GCed if needed. Step 7 Exercise 3:


Good practice is to release any resources that can be released during an onPause() as well, to take up less resources when in the passive state. For this reason we will close the DBHelper class and set the field to null so that it can be garbage collected if necessary. onResume() on the other hand, will re-create the mDbHelper instance so we can use it, and then read the note out of the database again and populate the fields.



This however is not implemented in the code that comes with the tutorial. Once you are comfortable with what is explained go ahead and act on the advice. Close the database connection and set it to null when handling the OnPause()

@Override
protected void onPause() {
super.onPause();
if (mDbHelper != null) {
mDbHelper.close();
mDbHelper = null;
}
}


Go ahead and run your application you will find the app ending in Null pointer exception right after you are done editing the note. To learn what is going on implement the override methods for other lifecycle events – such as onFreeze, onResume etc. Set the debugger on and go through the code (without closing the database connection and setting it to null). You will witness the events exactly as explained in the tutorial and documentation given for Activity. Yet as soon as you introduce the code above under OnPause() it goes haywire. The reason for the error is another method - startManagingCursor(), introduced in Step 12 of Exercise 1 with the following explanation –


After obtaining the Cursor from mDbHelper.fetchAllNotes() we use a method called startManagingCursor() from activity to allow Android to take care of the Cursor lifecycle instead of us needing to worry about it. We will cover the implications of the lifecycle in exercise 3, but for now just know that this allows Android to do some of our resource management work for us.


What’s going on is that when you close database connection and set it to null you are interfering with what this function is intended to do. As the app Activity comes back to life Android tries to ‘restore’ state of your cursor for you and finds the database connection was closed and ends up throwing an exception(!?). So if you remove call to this function you will be put back in driving seat. Somewhere in the documentation it says Android is/will be providing more such state management functions (more on it later) and one has to be taking advantage of these for better performing nimble Android apps.

Monday, June 16, 2008

Helping around the house

Now she is all excited if asked to help. Always ready to lend a hand to her mom here she is working in the kitchen while mom is preparing morning tea.

Saturday, June 14, 2008

I'm on swings




Three Column Modification courtesy of The Blogger Guide