Friday, September 30, 2011

Android Programming - Activity


As already presented in earlier sections, Android application components can consist of 4 things, Activities, Services, Broadcast Receivers and Content Providers. For Activity itself has a cycle as shown above. So like in the Hello Android example, class that inherit Activity (extends) can also override the other method - in addition to the OnCreate method:
  1. There onStart to be run when the program starts running,
  2. onResume that will be executed when the program has resumed (from another screen),
  3. onPause that will be executed when the program loses focus (replaced by another screen),
  4. onStop when the program was stopped by the system,
  5. onDestroy when the program eventually will be completely kill.


Before getting into examples of Activity and its use, I think we should discuss first about  Intent and Intent Filters as the trigger passage of an Activity. Intent is a facility in the message delivery system of Android applications. Intent object itself is a passive data structure that contains an abstract description of the operation to be executed. Intent may contain:
  1. The name of the component that will handle these Intent
  2. Action: a string that conveys Action to be performed
  3. Data: URI (Uniform Resource Identifier) ​​of data to be given the Action and the MIME type of the data
  4. Category: String containing the category of components such as what is going to handle Intent
  5. Extras: key-value pairs that must be delivered Intent to components that will handle these Intent
  6. Flags: as a marker for example how to run the Activity or how the treatment after the activity is executed.


Intent can be both explicit and implicit. Explicit Intent stating the name of the component that will be going to handle it, while Implicit Intent is not, Android will then try to find the right components for it to handle Intent by looking at the Intent Filter in file AndroidManifest.xml (as we did in Example Hello Android). Each Activity, Service or Broadcast Receiver that has a declaration of Intent filer in its AndroidManifest could have the chance to run the Intent when the filter is appropriate (as seen from Action, Data URI and MIME and categories), while an Activity that have no IntentFilter declaration can only run the by explicit Intent.

Let's begin the example. All of the source code available on my Github. For this example you can refer to this link : LearnActivity on GitHub. (sorry to not provide the code here, I'm really bored and stressed to write code in Blogger, it's so not easy and difficult.. -__-).


1. Create new project, named it LearnActivity. I use SDK 2.2 Froyo, you can use the other, maybe in some code will slightly change, but overall will be Ok.


2. Open your AndroidManifest file. Create two new Activity, named as you wish, I used Activity2 and Activity3 instead, for simplicity. Your AndroidManifest will be like this, and remain like this till the end, because we won’t change anything.


3.  Next Step we will create layout for each Activity, not each actually, the Activity3 will have no layout, so we will create to layout, main.xml and activity2.xml.
Main.xml is layout for MainActivity. It consist of one LinearLayout that contain two TextView and two Button. This the preview template :


Activity2 will consist one LinearLayout with two TextView and one Button. This is the preview

4. You will see some error mark in your XML code, this because of no String resource available, let’s create it. Edit your value/strings.xml to look like mine.
5. MainActivity is the main gate of our application, it will start first when application was launched. I give two main task for this Activity, Logging an Activity Life Cycle and Starting Activity2 via the button click.
It will create a log in DDMS, with tag LifeCycleLog, containing each activity life cycle. I override the onCreate, onPause, onStart, onStop, onDestroy and onRestart method from class Activity. For each lifecycle it will show you in DDMS screen, what phase is your Activity running now.
Next I create two public method to use function act2 and ac2ext, like we have write before in each button onClick method. Act2 method will start an Activity2 via Intent with no extras, and act2ext will start Activity2 with little extras text. We will see the difference in Activity2 screen. 
6. Activity2 will have two main function too. It will start Activity3 for testing ActivityForResult and show the difference between starting activity with extras and without. 
It will check for extra, if available will change the textview text, if not it will do nothing. And then it will use result function for button onClick to start Activity3, trying to receive the result. If result code == 1, it will toast result received, if not it will toast failed to receive result.
7. Activity3 have no layout. It just received request from activity2 and then start the activity2 again with result code.
There you can set result code, change the number 1 to any other number, and it will show you failed toast.
8.  It’s done. Let’s test your application. You can use emulator or real device, it doesn’t matter. To see the log of life cycle, head to the DDMS screen, it shows you clearly what phase your app has running for now. Check each activity, I’ve found no error, but maybe you will, if error happen, please double check your code first.

Categories: , , , ,

1 komentar:

Avatar Html5 Video Player said...

Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write ups thanks once again.

Copyright © Johannes Dwi Cahyo | Powered by Blogger

Design by Anders Noren | Blogger Theme by NewBloggerThemes.com | BTheme.net      Up ↑