An Activity in an android application is an interface that the user interacts with. It is the only thing that the user can actually see and interact with in any android application. It is just like a form or window --that you always see-- in your laptop or personal computer. An android application may contain a single activity or a group of many activities linked together (by using Intents) for presentation of interface and data. In this article I will explain you how you can create an android activity for your application.

Though a default activity is already created when you create your android package, you might need more activities in your android application. Here I have listed out most important points and steps to remember and follow when you create an activity in your android application or game.

1. Create a  new class and sub-class it as Android Activity. To do this simply extends the class as Activity.
  • Create a new class by going to File>>New and select Class. Give your class (activity) an appropriate  name and proceed by clicking OK button.
  • Now, extend your java class as Activity. If your class name is Myactivity replace the code public class Myactivity { with public class Myactivity extends Activity { . You will need to import Android.Activity class to your project. To do this simply press Ctrl + Shift + O and Eclipse will import it for you.
2. Add  unimplemented methods. onCreate(Bundle) is one of the required method for Activity class as it first called when the activity is created. So, go ahead and add it to your project. You can add the code manually or let Eclipse add it for you.
  • Type this code to add it manually:
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState); /* This will load previously saved instance of the Activity if any. */
  • To add it automatically go to Source>>Override/Implement Methods in Menu Bar and choose onCreate(Bundle) and press Ok button.
3. Set the content/views to be displayed in this Activity by calling setContentView(int) with a layout defining your UI. You will need to define your layout in a separate xml file and keep it inside layout folder in Res directory.
  • If your layout is named main.xml, then add this code in your onCreate(Budle) method we called earlier. We need to inflate (display) our layout when our activity is first created, so we place it inside onCreate(Bundle) method.
    setContentView(; /* Gets the layout and views from main.xml file and inflates it. */

    R represents res directory of your project and id is the attribute that helps use recognize our views, layouts and other components uniquely.
4. You can add other [optional] methods to your activity if you want your own code to be executed when your activity is started, paused, resumed, stopped or destroyed. These are the life cycle of any Activity. If you don't have these method on your activities, Android System will handle how to deal with it in each case.
  • To add these activities you can use Eclipse (Source>>Override/Implement Methods...) option and select each one from the list available and press ok. Ecplise will create the methods (template/outline) for you.
  • Or, you can manually add these methods to your Activity as you did above with onCreate(Bundle). See the templates below. Generally onDestroy(), onPause() and onResume() are of more greater importance. Generally,  Large programs use these methods to minimize the use of CPU and resources, by using appropriate code on call of each method.
        protected void onDestroy() {
            /* Code to be executed when your activity is destroyed or closed. You can write code for memory clean up, save data for next time activity is created, etc here.*/
        protected void onPause() {
           /* Called when the system is about to start resuming a previous activity. You can use this method to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, etc.  Implementations of this method must be very quick because the next activity will not be resumed until this method returns.   */
        protected void onResume() {
           /* Called when the activity will start interacting with the user. At this point your activity is at  the top of the activity stack, with user input going to it.  */
        protected void onStart() {
          /* Called when the activity is becoming visible to the user. */
        protected void onStop() {
          /* Called when the activity is no longer visible to the user, because another activity has been resumed and is covering this one. This may happen either because a new activity is being started, an existing one is being brought in front of this one, or this one is being   destroyed. */
          protected void onRestart() {
                Called after your activity has been stopped, prior to it being started again. 

    Note the @override annotation in every method we have created. It is
    used to mark methods that override a method declaration in a superclass.
5. These are not the only methods (or codes) you have on your Activity. You yet will have to write the code that you want to execute when users interact with (views/widgets in) your Activity. User interactions can be button click events, long press on any views, etc. Write the appropriate code for your program and then save it. You write all these codes inside onCreate(Bundle) method, so that your code/method can be called as your Activity loads. This way you can respond to your users even just after the activity is created --no need to wait for Pause, Resume, Start, events to occur to load the code.

6. You will need to add your activity to your application in AndroidManifest.xml file of your project.
  • Open AndroidManifest.xml file of your Android Project from Project Explorer.
  • Inside application tag in your xml file add your activity as follow:
    <activity android:label="@string/app_name"
              android:name=".Myactivity" >
  • Note that the first Activity --the one loaded when your applicaiton starts-- should contain the inten-filter to listen to the intent (signal) which the system sents when an app is started. To do this add this code within your activity tag in your AndroidManifest.xml file . This way you can take control of which activity you want to load first when the app is started.
    <activity android:label="@string/app_name"
              android:name=".Myactivity" >
                <intent-filter >
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
Save everything up and then run (Ctrl + F11) your android application to see the results --Wow! Finally the fruit of the hard work. Enjoy creating as much activites as you want. For more detailed information on Activity in an android application please consider visiting the android developer's website. Do you still have any doubts? Ask in the comment below.

Post a Comment Blogger Disqus

Please share your view(s) through the comment(s) below.
Thank You!