Android-Activities Understanding
Understanding Activities
To create an activity, 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);
}
}
Android-Activities Understanding
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:
android:versionCode=”1”
android:versionName=”1.0”>
Android-Activities Understanding
The Activity base class defines a series of events that governs the life cycle of an activity. The Activityclass 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 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