Sunday 24 July 2011

Two birds ...

My last post was about a problem with returning data from one activity to another. The post before suggested that I needed to understand the sequence of events in using an activity. Understanding the sequence of events has helped by solve the problem.

onActvityResult() does indeed get the data returned from the second activity. The fact that a second activity hid the first, meant that the first activity was forced to go though its standard sequence of events to display itself, and in there I loaded the data from the database overwriting what had been returned from the second activity.

The sequence of events is well documented - I just didn't appreciate the significance. Still, I have learned some more and experimented with using an object to hold the data for an activity. More of that later.

Friday 22 July 2011

onActivityResult

If you want to launch a new activity from an activity the startActivity() works. If you want to return a result from the second activity then startActivityForResult() also works. The result is passed back in an intent which is read by overriding onActivityResult in the first activity. Once again that seems to work.

However, if I want to use the returned info to update a textview (or an editview) it does not change. I have used .invalidate() to force a redraw, but still nothing changes. I have checked the returned info by writing a log entry and by displaying a toast, both show the data has changed, but the display doesn't change. I have no ideas why. I have wasted a couple of hours on this, so I give up.

Wednesday 20 July 2011

Getting started

I've started to use Android more and more. Building my own app seemed like the next step, so I've had a go. I have decided to write a blog, partly as a record of progress, partly as a way to write down useful stuff and partly to help clear my thinking.

Developing in isolation can be hard work, especially learning a new language or a new API. The Android environment is substantial and there's plenty to learn. I've read a few books - the best of which is the Professional Android 2 Application Development by Reto Meier. The books got me started and gave me enough information to start to write stuff for myself - which is where the real learning begins.

Android development is usually done with Java; actually a Java-like implementation known as Dalvik, but very much like Java.  I followed the recommendations of more than one place and installed Eclipse (on Ubuntu). It works well enough and I am getting used to it. I have written stuff in Java before, but not extensively. It is quite funny when the advice tells me to be very careful about memory use, the first home computer I had only had 4k of memory so an Android phone is huge by comparison. The first mainframe I wrote programs for had little more memory than that and almost no available disk space, all big files were on tape. I have long grumbled about the wasteful way programs are written, so I should be at home here.

The terms and names are quite different from other environments, but that is normal. The place information is displayed or edited is an activity. This might be a form, screen, window, frame or page elsewhere. The means of communicating between activities, and more widely uses an intent. This could be a message elsewhere. Intents are flexible, with an open-ended way of attaching data to the intent. My biggest niggle with them so far is that it is not easy to pass an object with an intent.

The recommended way of creating an activity layout is to specify an XML file that defines the layout. In fact the recommended way of specifying everything is in an XML file, so text in messages and the titles of fields should be all specified in an XML file. Part of the reason for this is that there can be multiple versions of these XML files for different device sizes and different locales and languages. This seems like a great idea, but I know that translating something is not as simple as substituting translated words. Maybe it will work well.

So far the simple stuff I've written I've stored locally and backed up to a USB disk. I think I may look into using github from Eclipse. That would provide a way to share anything that is useful and provide another copy off-site.

My next task is to understand the activity life cycle a bit better. It is vital to know what happens and when because the life cycle is not in the hands of the programmer, which feels a bit odd. I also want to understand the best way to store data being used by an activity; is that an object created for the purpose, or fields of the activity, or data attached to an intent or something I haven't seen yet. In such a heavily object-biased language as Java, I'll start there.