Android 的活動是可以層疊的,我們每啟動一個新的活動,就會覆蓋在原活動之上,然後點擊back鍵會銷毀最上面的活動,下面的一個活動就會重新顯示出來。
其實Android是使用任務(task)來管理活動的,一個任務就是一組存放在棧裏的活動的集合,這個棧也被稱為返回棧(Back Stack)。棧是一種後進先出的數據結構,在默認的情況下,每當我們啟動一個新的活動,它會在返回棧中入棧,並處於棧頂的位置。每當我們按下Back鍵或者調用finish()方法銷毀一個活動的時候,處於棧頂的活動會出棧,這時前一個入棧的活動就會重新處於棧頂的位置。系統總是會顯示棧頂的活動給用戶。
每個活動在其生命週期內最多可能會有四種狀態:
1、運行狀態;
當一個活動位於返回棧的棧頂時,這個活動就處理運行狀態。系統最不願意回收的就是處理運行狀態的活動,這會給用戶帶來非常差的用戶體驗
2、暫定狀態;
當一個活動不再處理棧頂位置但仍然可見時,這個活動就進入了暫停狀態。處於暫停狀態狀態的活動仍然是完全存活著的,系統也不願意去回收這種活動(因為它還是可見的,回收可見的東西都會在用戶體驗方面有非常不好的影響),只有在內存極低的情況下,系統才會去回收這種活動
3、停止狀態;
當一個活動不再處於棧頂位置且該活動完全不可見的時候,就進入了了停止狀態。系統仍會為這種活動保存相應的狀態和成員變量,但是這並不是完全可靠的,但其他地方需要內存時,處理停止狀態的活動有可能被系統回收
4、銷毀狀態。
當一個活動從棧中移除後就變成了銷毀狀態,系統會最傾向與回收處於銷毀狀態的活動,從而保證手機內存的充足。
Activity類中定義了七個回調的方法,覆蓋了活動生命週期內的每一個環節。
1、onCreate()
每個活動都需要重寫該方法,它會在活動第一次被創建的時候調用。我們應該在該方法中完成初始化,比如:加載佈局、綁定事件等。
2、onStart()
這個方法將活動由不可見變為可見。
3、onResume()
這個方法在活動準備好和用戶進行交互的時候調用,這時的活動一定處於返回棧的棧頂,並且處於運行狀態。
4、onPause()
這個方法在系統準備去啟動或者恢復另一個活動的時候調用,我們一般會在這個方法裏面將一些消耗CPU的資源釋放掉,以及保存一些關鍵數據。但這個方法的執行一定要快,不然會影響到新的棧頂活動的使用。
5、onStop()
這個方法在活動完全不可見的時候調用。它和onPause()方法的主要區別在於,如果啟動的新活動是一個對話框式的活動,那麼onPause()方法會被執行,onStop()不會被執行。
6、onDestroy()
這個方法在活動被銷毀前調用,之後活動的狀態將變成銷毀狀態。
7、onRestart()
這個方法將活動由停止狀態變成運行狀態前調用,也就是活動被重新啟動。
七個方法中,除onRestart()外,其他的都是兩兩成對,從而可以把活動分為三種生存期:
1、完整生存期:
從onCreate()到onDestroy()之間所經歷的就是完整生存期,一般情況下活動在onCreate()完成初始化,在onDestroy()釋放內存。
2、可見生存期:
從onStart()到onStop()之間所經歷的就是可見生存期。在這個生存期的活動,對於用於總是可見的,即便有可能無法與用戶進行交互。我們可以通過這兩個方法,合理的管理那些對用戶可見的資源,如在onStart()方法中對各種資料進行加載,而在onStop()中對資料進行釋放。而且保證處理停止狀態下的活動不會佔用太多的內存。
3、前臺生存期:
從onResume()和onPause()方法之間所經歷的,就是前臺生存期。在前臺生存期內,活動總是處於運行狀態的,此時的活動是可以和用戶進行交互的。
MainActivity代碼如下:
package com.example.activitylifecycletest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
public class MainActivity extends Activity {
String log = "Demo_log";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
Log.d(log, "This is main_activity onCreate");
Button button1_1 = (Button)findViewById(R.id.button1_1);
Button button1_2 = (Button)findViewById(R.id.button1_2);
button1_1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
Log.d(log, "start second activity");
}
});
button1_2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
Log.d(log, "finish main activity");
}
});
}
protected void onStart(){
super.onStart();
Log.d(log, "This is main_activity onStart");
}
protected void onResume(){
super.onResume();
Log.d(log, "This is main_activity onResume");
}
protected void onPause() {
super.onPause();
Log.d(log, "This is main_activity onPause");
}
protected void onStop(){
super.onStop();
Log.d(log, "This is main_activity onStop");
}
protected void onDestroy(){
super.onDestroy();
Log.d(log, "This is main_activity onDestroy");
}
protected void onRestart(){
super.onRestart();
Log.d(log, "This is main_activity onRestart");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
main_activity 定義兩個button,一個開啟下一個活動(send),一個stop該活動(stop):
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.activitylifecycletest.MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="25dp"
android:text="Send" />
<Button
android:id="@+id/button1_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1_1"
android:layout_alignBottom="@+id/button1_1"
android:layout_marginLeft="56dp"
android:layout_toRightOf="@+id/textView1"
android:text="Stop" />
</RelativeLayout>
新增SecondActivity方法:
package com.example.activitylifecycletest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SecondActivity extends Activity{
String log = "Demo_log";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_second);
Log.d(log, "This is second_activity onCreate");
Button button2_1 = (Button)findViewById(R.id.button2_1);
button2_1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
Log.d(log, "finish second activity");
}
});
}
protected void onStart(){
super.onStart();
Log.d(log, "This is second_activity onStart");
}
protected void onResume(){
super.onResume();
Log.d(log, "This is second_activity onResume");
}
protected void onPause() {
super.onPause();
Log.d(log, "This is second_activity onPause");
}
protected void onStop(){
super.onStop();
Log.d(log, "This is second_activity onStop");
}
protected void onDestroy(){
super.onDestroy();
Log.d(log, "This is second_activity onDestroy");
}
protected void onRestart(){
super.onRestart();
Log.d(log, "This is second_activity onRestart");
}
}
該頁面只有一個 返回的button(return):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button2_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="67dp"
android:text="Return" />
</RelativeLayout>
啟動虛擬機,logcat 內容呈現如下:
1、mainactivity 啟動時,執行:onCreate() –> onStart() –> onResume();
2、執行send button時,mainactivity執行:onPause() –> onStop(),secondactivity執行:onCreate() –>onStart() –> onResume();
3、執行return button時,secondactivity執行:onPause()–> onStop() –> onDestroy(),mainactivity執行:onRestart() –> onStart() –> onResume();
4、執行stop button時,mainactivity執行:onPause() –> onStop() –> onDestroy()。
執行鍵盤上的Back鍵,mainactivity執行:onPause() –> onStop() –> onDestroy()
執行鍵盤上的home鍵,mainactivity執行:onPause() –> onStop()
當再次執行應用程序時,mainactivity執行:onRestart() –> onStart() –> onResume()
執行home鍵時,可以在onPause()方法中將文本框的數據取出,再執行應用程序時,在onRestart()方法中將原來的信息還原至文本框中。