Sunday, 14 April 2013

How to Create and Start Activity in Android Application


                                                 ACTIVITY
An activity is a window that contains the user interface of your applications.  Typically, applications have one or more activities, and the main aim of an activity is to interact with the user. From the moment an activity appears on the screen to the moment it is hidden, it goes through a number of stages, known as an activity’s life cycle.
                                             
                                                        HOW TO CREATE AN ACTIVITY:
1---
To create an activity, you create a Java class that extends the Activity base class:

 
package com.sartaj.Activities;   //Package name
import android.app.Activity;    //import Activity Package
import android.os.Bundle;       // import Bundle package
 public class MainActivity extends Activity { //Inharit the Activity Class
/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
}}


Your activity class would then load its UI component using the XML file defined in your res/layout folder. In this example, you would load the UI from the main.xml file using-

setContentView(R.layout.main);

Every activity you have in your application must be declared in your AndroidManifest.xml file, like this:

<?xml version=”1.0” encoding=”utf-8”?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.sartaj.Activities”
android:versionCode=”1”
android:versionName=”1.0”>
<application android:icon=”@drawable/icon”
android:label=”@string/app_name”>
<activity android:name=”.MainActivity”
android:label=”@string/app_name”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category
android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion=”9” />
</manifest>

The Activity base class defines a series of events that governs the life cycle of an activity. The Activity class defines the following events:
➤➤ onCreate() — Called when the activity is first created
➤➤ onStart() — Called when the activity becomes visible to the user
➤➤ onResume() — Called when the activity starts interacting with the user
➤➤ onPause() — Called when the current activity is being paused and the previous activity is being resumed
➤➤ onStop() — Called when the activity is no longer visible to the user
➤➤ onDestroy() — Called before the activity is destroyed by the system (either manually or by the system to conserve
                                memory)
➤➤ onRestart() — Called when the activity has been stopped and is restarting again

By default, the activity created for you contains the onCreate() event. Within this event handler is the code that helps to display the UI elements of your screen.
                                 


0 comments:

Post a Comment

 

© 2011 Reach 2 Android - Tutorials on Android and PHP - Designed by Mukund | ToS | Privacy Policy | Sitemap

About Us | Contact Us | Write For Us