Activities
Activity is a single screen or a window, which provides an interface (UI), so user can interact with the system. It is same like Window or frame in java e.g. Dialing a number etc.
An Application consists of multiple activities, Typically one activity is specified as "main" Activity, which is present to the user when the application is launched for first time. Each activity can start another activity to perform different actions. when an activity is launched it is pushed to the back stack (it works on 'Last in First out' mechanism). so when the user is done with the current activity and he/she presses the back button the current activity is popped from the stack (is destroyed) , and the previous Activity resumes. when an Activity is stopped or started, it is notified of this change in state through activity life-cycle callback methods.
Activity Life-cycle
If you are familier with java/c++ etc then you must have understanding of 'main' method which is called first while executing a program, same concept is here when an activity is launched the onCreate() method is called first, there is a sequence of callback methods in android which are called while starting to the tear down of an Activity.
onCreate(): It is Called when the activity is first created. here you should do all of your normal static set up: create views, initialize stuffs, etc. Always followed by onStart().
onRestart(): It is Called after your activity has been stopped, prior to it being started again. Always followed by onStart()
onStart(): It is Called when the activity is becoming visible to the user. Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden.
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. Always followed by onPause().
onPause ():Called when an activity is going into the background, but has not (yet) been killed. The counterpart to onResume().
onStop():Called when you are no longer visible to the user. You will next receive either onRestart(), onDestroy(), or nothing, depending on later user activity.
onDestroy():It is called when your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.
Example
Create a new project, if you can't then visit the previous tutorial. use package name "com.example.activity.lifecycle" .
MainActivity.java
package com.example.activity.lifecycle;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
public class MainActivity extends Activity {
String msg = "Lifecycle :
";
/** onCreate() methos is Called when the activity is first
created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(context, "The onCreate() event", Toast.LENGTH_LONG).show();
Log.d(msg, "The onCreate() event");
}
/**
onStart() method is Called when the activity is about to become visible. */
@Override
protected void onStart() {
super.onStart();
Toast.makeText(context, "The onStart() event", Toast.LENGTH_LONG).show();
Log.d(msg, "The onStart() event");
}
/**
onResume() method is Called when the activity has become visible. */
@Override
protected void onResume() {
super.onResume();
Toast.makeText(context, "The onResume() event", Toast.LENGTH_LONG).show();
Log.d(msg, "The onResume() event");
}
/**
onPause() method is Called when another activity is going to backgound. */
@Override
protected void onPause() {
super.onPause();
Toast.makeText(context, "The onPause() event", Toast.LENGTH_LONG).show();
Log.d(msg, "The onPause() event");
}
/**
onStop() method is Called when the activity is no longer visible. */
@Override
protected void onStop() {
super.onStop();
Toast.makeText(context, "The onStop() event", Toast.LENGTH_LONG).show();
Log.d(msg, "The onStop() event");
}
/**
onDestroy() method is Called just before the activity is destroyed. */
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(context,"The onDestroy() event", Toast.LENGTH_LONG).show();
Log.d(msg, "The onDestroy() event");
}
}
All the UI components using xml file (res/layout/*) are loaded by activity class using setContentView() method.
setContentView(R.layout.activity_main);
Every activity used by your application must be declared in your AndroidManifest.xml file
and the main activity for your app must be declared in the manifest with an <intent-filter> that
includes the MAIN action and LAUNCHER category which will be started when your app launch:
Manifes file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activity.lifecycle"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="22" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
hints
When the Activity first time loads the events are called as below:
onCreate()
onStart()
onResume()
When you click on Phone button the Activity goes to the background and the below events are called:
onPause()
onStop()
Exit the phone dialer and the below events will be called:
onRestart()
onStart()
onResume()
When you click the back button OR try to finish() the activity the events are called as below:
onPause()
onStop()
onDestroy()

No comments:
Post a Comment