Monday, February 20, 2012

Android application components and developing our first app (I)

Here we are again. In the previous entry of this blog we prepared the working environment to android app programming on Ubuntu. Therefore in this post we'll make our first android application using this environment. But, before we start to program, we should know about how android apps work.

BASIC ANDROID APP COMPONENTS


These are Activities, Intents, Views, Services, Content Providers, Broadcast Receivers and finally the Manifest. In the following paragraphs I will briefly describe each one to start programming as soon as possible:

ACTIVITIES

This is the main component of an android app. The main process is usually done here, or, at least, it starts from here. We could say an activity is like  a screen on our application, moreover we can only have one active activity at the same time. So an android application usually consists of several loosely-related activities.
We could see that like a stack. Top activity would be the active one, and we only would see this one. If we push another activity , this will be on top so this will be the active one. From this activity we can return to the previous one taking it out of the stack.

Stack



I told you application activities are loosely-related, because it was designed in that way, to avoid coupling and increase cohesion between application modules, and later we'll see that complex information sharing between activities is not easy.

Activities have a well-defined life cycle starting when activity is created (either by system call or by another activity call into the same application) until it's stopped or removed.

Activity life cycle

As we can see in this diagram, an activity goes trough several states during its life. You must know that when we stop an activity, android system do not destroy it immediately, but stops it, saving its resources in case it has to restart it. System will destroy the activity when deemed appropriate, either by memory requirement or by time running. So when you close an application in your device, doesn’t mean that this application is no longer in the memory.

Each time an activity changes state, it calls a method. You can see these methods on the diagram, onCreate(), onStart()...

So, when we will programming our activities, we'll be able to execute instructions that we need into each one of these methods. Therefore, looking at this diagram, we can see that, when we launch an activity (if it was not already in memory), method onCreate() will be automatically called, and after that,  onStart() method will be called, and then onResume() will be called...
When we leave an application or when activity goes to background, onPause() method will be called, and after that onStop() method will be called. Method onDestroy() will be called when the activity is finishing or being removed by the system, as I told, either by resource requirement or something similar.

If we pay attention to the diagram, we'll see that just before execute the main code in the activity, the system always will call onResume() method, and when we leave the activity onPause() always will be called. Therefore this two plus onCreate() will be the three most commonly used methods, although we don't have to forget the other ones.

To finish with this, I must say if we want to finish an activity by code, we can call the finish() method.

On the next entry I'll explain the rest of the application components and we'll make our first app.


No comments:

Post a Comment