Pages

Ads 468x60px

Sunday 30 June 2013

Android Activities

Android Activities

You will learn how to create Android Activities you create a Java class that extends the Activity base class :

package net.learn2develop.Activities;
import android.app.Activity;
import android.os.Bundle;
        public class MainActivity extends Activity
        {
               /** Called when the activity is first created. */
              @Override
               public 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:
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=”net.learn2develop.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.Figure Below shows the life cycle of an activity and the various stages it goes through — from when the activity is started until it ends



The best way to understand the various stages experienced by an activity is to create a new project, implement the various events, and then subject the activity to various user interactions.
 

2 التعليقات:

Unknown said...

thank you, helpful article

gitnet said...

thanx ..

Post a Comment

.

Education blog