Các bạn có thể downfile mình đính kèm cho tiện :p
Android có cơ chế quản lý các process theo chế độ ưu tiên. Các process có priority thấp sẽ bị Android giải phóng mà không hề cảnh báo nhằm đảm bảo tài nguyên.
1.Foreground process: là process của ứng dụng hiện thời đang được người dùng tương tác.
2.Visible process: là process của ứng dụng mà activity đang hiển thị đối với người dùng (onPaused() của activity được gọi).
3.Service process: là Service đang running.
4.Background process: là process của ứng dụng mà các activity của nó ko hiển thị với người dùng (onStoped() của activity được gọi).
5.Empty process: process không có bất cứ 1 thành phần nào active.
Theo chế độ ưu tiên thì khi cần tài nguyên, Android sẽ tự động kill process, trước tiên là các empty process.
Android Activity Life Cycle:
Như mình đã giới thiệu ở trên , Actitvity là thành phần quan trọng nhất và đóng vai trò chính trong xây dựng ứng dụng Android. Hệ điều hành Android quản lý Activity theo dạng stack: khi một Activity mới được khởi tạo, nó sẽ được xếp lên đầu của stack và trở thành running activity, các Activity trước đó sẽ bị tạm dừng và chỉ hoạt động trở lại khi Activity mới được giải phóng.
Activity bao gồm 4 state:
- active (running): Activity đang hiển thị trên màn hình (foreground).
- paused: Activity vẫn hiển thị (visible) nhưng không thể tương tác (lost focus). VD: một activity mới xuất hiện hiển thị giao diện đè lên trên activity cũ, nhưng giao diện này nhỏ hơn giao diện của activity cũ, do đó ta vẫn thấy được 1 phần giao diện của activity cũ nhưng lại không thể tương tác với nó.
- stop: Activity bị thay thế hoàn toàn bởi Activity mới sẽ tiến đến trạng thái stop
- killed: Khi hệ thống bị thiếu bộ nhớ, nó sẽ giải phóng các tiến trình theo nguyên tắc ưu tiên. Các Activity ở trạng thái stop hoặc pausedcũng có thể bị giải phóng và khi nó được hiển thị lại thì các Activity này phải khởi động lại hoàn toàn và phục hồi lại trạng thái trước đó.
Biểu đồ miêu tả Activity state

Vòng đời của Activity:
- Entire lifetime: Từ phương thức onCreate( ) cho tới onDestroy( )
- Visible liftetime: Từ phương thức onStart( ) cho tới onStop( )
- Foreground lifetime: Từ phương thức onResume( ) cho tới onPause( )
Khi xây dựng Actitvity cho ứng dụng cần phải viết lại phương thức onCreate( ) để thực hiện quá trình khởi tạo. Các phương thức khác có cần viết lại hay không tùy vào yêu cầu lập trình.
Nguồn : vietandroid.com
Tiếp theo chúng mình đi xây dựng thử 1 chương trình để hiểu rõ về những gì đã đề cập ở trên
Chúng ta sẽ có 2 class là MainActivity ( chứa 1 nút , khi nhấn vào nút này màn hình sẽ chuyển qua activity2)
Bước 1 : Tạo 1 class MainActivity ( class này sẽ hiển thị khi ứng dụng được khởi động )
Trước tiên tạo giao diện cho MainActivity , ở đây sẽ có 1 nút button , khi nhấn vào nút này sẽ chuyển qua màn hình activity2
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >
</LinearLayout>
<Button
android:id="@+id/btnChuyenActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chuyển activity" />
</LinearLayout>

package com.example.lifecycle;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button btnChuyenActivity;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnChuyenActivity = (Button) findViewById(R.id.btnChuyenActivity);
btnChuyenActivity.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Activity2.class));//Nhấn vào nút này sẽ chuyển qua activity2
}
});
Log.d("oncreat1","oncreate1");
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
Log.d("onStart1", "onStart1");//đánh dấu là 1 để biết đây là sự kiện của MainActivity :p
super.onStart();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
Log.d("onResume1", "onResume1");
super.onResume();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
Log.d("onPause1", "onPause1");
super.onPause();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
Log.d("onDestroy1", "onDestroy1");
super.onDestroy();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
Log.d("onStop1", "onStop1");
super.onStop();
}
}
OK , giờ chuyển qua activity2 :p
Nhớ khai báo thêm 1 activity nữa trong manifest.xml để nói cho nó biết mình có thêm 1 activity nữa tên là activity2 nhé :p
<activity
android:name=".Activity2"
android:label="@string/title_activity_activity2" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Tiếp theo tạo giao diện cho activity2

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="43dp"
android:layout_marginTop="58dp"
android:text="Đây là activity2" />
</RelativeLayout>
Viết lệnh cho activity2
package com.example.lifecycle;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
public class Activity2 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
Log.d("oncreat2","oncreate2");
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
Log.d("onStart2", "onStart2");
super.onStart();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
Log.d("onResume2", "onResume2");
super.onResume();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
Log.d("onPause2", "onPause2");
super.onPause();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
Log.d("onDestroy2", "onDestroy2");
super.onDestroy();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
Log.d("onStop2", "onStop2");
super.onStop();
}
}
OK , bây giờ chạy thử chương trình :p

Khi chương trình bắt đầu khởi động, đầu tiên sẽ gọi đến sự kiện onCreate -> onStart -> onResume
Bây giờ mình ấn nút chuyển sang activity 2 xem nó xảy ra những gì nữa nhé :p

Đúng như lý thuyết :p
paused: Activity vẫn hiển thị (visible) nhưng không thể tương tác (lost focus). VD: một activity mới xuất hiện hiển thị giao diện đè lên trên activity cũ, nhưng giao diện này nhỏ hơn giao diện của activity cũ, do đó ta vẫn thấy được 1 phần giao diện của activity cũ nhưng lại không thể tương tác với nó.
MainActivity đã bị Activity2 đè lên , nó xảy ra sự kiện pause1
Tiếp theo nó sẽ khởi tạo activity2 (onCreate-> onStart->onResume)
và activity2 đè lên hoàn toàn MainActivity -> xảy ra sự kiện onStop
stop: Activity bị thay thế hoàn toàn bởi Activity mới sẽ tiến đến trạng thái stop
Tiếp tục nhấn nút quay lại của máy ảo android

Lúc này MainActivity đang bị stop( stop chứ ko pải destroy nhé , stop là nó vẫn còn tồn tại trong bộ nhớ , lúc này nó resume lại là lại chạy bình thường , khỏi cần onCreate luôn )
Activity2 bị pause -> stop -> destroy ( destroy :) , vậy là nó đã biến mất khỏi bộ nhớ , bây giờ nếu nhấn nút chuyển activity , nó sẽ onCreate-> start ->resume) chứ ko phải chỉ đơn thuần là start->resume như MainActivity đâu( vi MainActivity chưa bị destroy , mainActivity vẫn nằm trong bộ nhớ mà ) . :p
File đính kèm :p :
http://www.mediafire.com/?pfsw8vko0a6g6so