An Activity
is an application component that provides a screen with which users can interact in order to do something, such as dial the phone, take a photo, send an email, or view a map.
An application usually consists of multiple activities
Each time a new activity starts, the previous activity is stopped, but the system preserves the activity in a stack (the "back stack").
when the user is done with the current activity and presses the Back button, it is popped from the stack (and destroyed) and the previous activity resumes
activity's lifecycle callback methods
There are several callback methods that an activity might receive, due to a change in its state—whether the system is creating it, stopping it, resuming it, or destroying it—and each callback provides you the opportunity to perform specific work that's appropriate to that state change
To create an activity, you must create a subclass of Activity
(or an existing subclass of it).
package com.cmge;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
/**
* @desc 一个简单的Activity
* @author ljt
* @time 2014年8月24日 下午10:42:19
*/
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
The two most important callback methods are:
You must implement this method.
The system calls this when creating your activity
this is where you must call setContentView()
to define the layout for the activity's user interface.
public void setContentView (View view)
Set the activity content to an explicit view. This view is placed directly into the activity's view hierarchy
The system calls this method as the first indication that the user is leaving your activity
This is usually where you should commit any changes that should be persisted beyond the current user session (because the user might not come back).
Declaring the activity in the manifest
You must declare your activity in the manifest file in order for it to be accessible to the system
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cmge" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.cmge.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> </manifest>
Theandroid:name
attribute is the only required attribute—it specifies the class name of the activity
Using intent filters
using the <intent-filter>
element—in order to declare how other application components may activate it.
The <action>
element specifies that this is the "main" entry point to the application.