關於activity的生命週期1
anctivity的生命週期比較複雜,但是很好的理解其生命週期,對於我們設計效能優良的應用程式有很大的幫助。
下面是官方文件上的一幅比較經典的activity生命週期示意圖:
其中涉及到七個生命週期函式,下面摘自官方文件的介紹:
文件上已經很瞭然介紹了各函式的作用,什麼時候被呼叫等,下面簡單的總結一下:
1.onCreate()
Activity首次被建立時被呼叫,用於設定activity的佈局檔案,繫結按鈕監聽器等一些靜態操作
2.onStart()
Activity對使用者可見時被呼叫
3.onResume()
Activity獲得使用者焦點,即使用者可以操作activity時被呼叫
4.onPause()
應用程式啟動了其他activity時被呼叫。一般用於當前activity中的資料
5.onStop()
activity不可見時被呼叫
6.onRestart()
已停止的activity重新啟動被呼叫
7.onDestroy()
呼叫activity的finish()方法或者android系統資源不足的時候被呼叫
我們透過簡單的實驗來說明一下:
下面的實驗是建立兩個activity,跳轉的過程中,透過activity生命週期函式來說明activity的生命過程。由於佈局很簡單,下面只給出截圖:
activity_main.xml:
two.xml:
MainActivity:(下面透過重寫activity生命週期函式,來列印各個過程)
TwoActivity:
此時我們透過執行程式,透過Logcat可以看出列印出的各個生命週期階段:
啟動後,日誌輸出:(第一個activity經歷了oncreate,onstart,onresume,獲得焦點)
點選按鈕然後進入第二個activity:(第一個activity可以是可見,但是失去了焦點,第二個activity獲得了焦點)
此時點選back鍵:(此時第二個activity會失去焦點。而第一個activity之前被stop,此時會onrestart,重新獲得焦點。從上面的activity生命圖解可以很清楚的看出整個過程。這方面涉及到activity和Task相關知識,task以棧的形式存在,先啟動的activity會先入棧,最後啟動的activity會放在棧頂。上面第二個activity進棧的時候,此時完全遮蓋了第一個activity,所以導致了第一個activity被onstop,但是沒有被銷燬,這也是當第二個activity出棧,失去焦點後,第一個activity restart,而沒有oncreate的原因)
下面是官方文件上的一幅比較經典的activity生命週期示意圖:
其中涉及到七個生命週期函式,下面摘自官方文件的介紹:
Method | Description | Killable? | Next | ||
---|---|---|---|---|---|
onCreate() |
Called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity's previously frozen state, if there was one.
Always followed by onStart(). |
No | onStart() | ||
onRestart() |
Called after your activity has been stopped, prior to it being started again.
Always followed by onStart() |
No | onStart() | ||
onStart() |
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. |
No | onResume()or onStop() | ||
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(). |
No | onPause() | ||
onPause() |
Called when the system is about to start resuming a previous activity. This is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, etc. Implementations of this method must be very quick because the next activity will not be resumed until this method returns.
Followed by either onResume() if the activity returns back to the front, or onStop() if it becomes invisible to the user. |
Yes |
onResume()or onStop() |
||
onStop() |
Called when the activity is no longer visible to the user, because another activity has been resumed and is covering this one. This may happen either because a new activity is being started, an existing one is being brought in front of this one, or this one is being destroyed.
Followed by either onRestart() if this activity is coming back to interact with the user, oronDestroy() if this activity is going away. |
Yes |
onRestart()or onDestroy() |
||
onDestroy() | The final call you receive before 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. | Yes |
nothing |
1.onCreate()
Activity首次被建立時被呼叫,用於設定activity的佈局檔案,繫結按鈕監聽器等一些靜態操作
2.onStart()
Activity對使用者可見時被呼叫
3.onResume()
Activity獲得使用者焦點,即使用者可以操作activity時被呼叫
4.onPause()
應用程式啟動了其他activity時被呼叫。一般用於當前activity中的資料
5.onStop()
activity不可見時被呼叫
6.onRestart()
已停止的activity重新啟動被呼叫
7.onDestroy()
呼叫activity的finish()方法或者android系統資源不足的時候被呼叫
我們透過簡單的實驗來說明一下:
下面的實驗是建立兩個activity,跳轉的過程中,透過activity生命週期函式來說明activity的生命過程。由於佈局很簡單,下面只給出截圖:
activity_main.xml:
two.xml:
MainActivity:(下面透過重寫activity生命週期函式,來列印各個過程)
點選(此處)摺疊或開啟
-
package com.example.android02;
-
-
import org.apache.commons.logging.Log;
-
-
import android.app.Activity;
-
import android.content.Intent;
-
import android.os.Bundle;
-
import android.view.View;
-
import android.view.View.OnClickListener;
-
import android.widget.Button;
-
import android.widget.TextView;
-
-
public class MainActivity extends Activity {
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
//Log.i(TAG,"onCreat");
-
TextView text = (TextView)findViewById(R.id.text1);
-
text.setText(R.string.number1);
-
Button button = (Button)findViewById(R.id.one);
-
button.setText(R.string.change);
-
button.setOnClickListener(new wang());
-
-
}
-
-
class wang implements OnClickListener{
-
-
public void onClick(View v){
-
-
Intent intent = new Intent();
-
intent.setClass(MainActivity.this,TwoActivity.class);
-
MainActivity.this.startActivity(intent);
-
-
}
-
-
//public
-
-
-
}
-
-
protected void onStart() {
-
super.onStart();
-
System.out.println("first activity --》 onstart");
-
// The activity is about to become visible.
-
}
-
@Override
-
protected void onResume() {
-
super.onResume();
-
System.out.println("first activity -->onresume");
-
// The activity has become visible (it is now "resumed").
-
}
-
@Override
-
protected void onPause() {
-
super.onPause();
-
System.out.println("first activity -->onpause");
-
// Another activity is taking focus (this activity is about to be "paused").
-
}
-
@Override
-
protected void onStop() {
-
super.onStop();
-
System.out.println("first activity -->onstop");
-
// The activity is no longer visible (it is now "stopped")
-
}
-
protected void onRestart(){
super.onRestart();
System.out.println("first activity -->restart");
}
-
@Override
-
protected void onDestroy() {
-
super.onDestroy();
-
System.out.println("first activity -->ondestory");
-
// The activity is about to be destroyed.
-
}
- }
TwoActivity:
點選(此處)摺疊或開啟
-
package com.example.android02;
-
-
import android.app.Activity;
-
import android.os.Bundle;
-
import android.widget.Button;
-
import android.widget.TextView;
-
-
public class TwoActivity extends Activity{
-
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.two);
-
TextView text = (TextView)findViewById(R.id.text2);
-
text.setText(R.string.number2);
-
@SuppressWarnings("unused")
-
Button button = (Button)findViewById(R.string.button2);
-
}
-
-
protected void onStart() {
-
super.onStart();
-
System.out.println("second activity --》 onstart");
-
// The activity is about to become visible.
-
}
-
@Override
-
protected void onResume() {
-
super.onResume();
-
System.out.println("second activity -->onresume");
-
// The activity has become visible (it is now "resumed").
-
}
-
@Override
-
protected void onPause() {
-
super.onPause();
-
System.out.println("second activity -->onpause");
-
// Another activity is taking focus (this activity is about to be "paused").
-
}
-
@Override
-
protected void onStop() {
-
super.onStop();
-
System.out.println("second activity -->onstop");
-
// The activity is no longer visible (it is now "stopped")
-
}
-
protected void onRestart(){
super.onRestart();
System.out.println("second activity -->restart");
}
-
@Override
-
protected void onDestroy() {
-
super.onDestroy();
-
System.out.println("second activity -->ondestory");
-
// The activity is about to be destroyed.
-
}
- }
此時我們透過執行程式,透過Logcat可以看出列印出的各個生命週期階段:
啟動後,日誌輸出:(第一個activity經歷了oncreate,onstart,onresume,獲得焦點)
點選按鈕然後進入第二個activity:(第一個activity可以是可見,但是失去了焦點,第二個activity獲得了焦點)
此時點選back鍵:(此時第二個activity會失去焦點。而第一個activity之前被stop,此時會onrestart,重新獲得焦點。從上面的activity生命圖解可以很清楚的看出整個過程。這方面涉及到activity和Task相關知識,task以棧的形式存在,先啟動的activity會先入棧,最後啟動的activity會放在棧頂。上面第二個activity進棧的時候,此時完全遮蓋了第一個activity,所以導致了第一個activity被onstop,但是沒有被銷燬,這也是當第二個activity出棧,失去焦點後,第一個activity restart,而沒有oncreate的原因)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29876893/viewspace-1815730/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- View生命週期與Activity生命週期的關係View
- Activity 知識梳理(1) Activity生命週期
- Activity生命週期
- Activity生命週期onDestroy
- [Android]Activity的生命週期Android
- activity的生命週期(總結)
- View和Activity的生命週期View
- 關於Fragment的生命週期Fragment
- Android Activity生命週期Android
- Activity生命週期總結
- activity生命週期的onPause和onStop
- 深入學習Activity生命週期
- Activity簡介及生命週期
- Activity的生命週期和啟動模式模式
- Activity生命週期與啟動模式模式
- Android 元件系列-----Activity生命週期Android元件
- Activity生命週期深入理解2
- Android Activity生命週期詳解Android
- Android四大元件——Activity——Activity的生命週期Android元件
- Android Activity生命週期的一點感悟Android
- 多Activity切換的生命週期問題
- 初識Android之Activity的生命週期Android
- Activity 生命週期和棧(Task)的關係及Intent 常用的FlagsIntent
- Activity橫豎屏切換生命週期
- Android全面解析之Activity生命週期Android
- 喜聞樂見之Activity生命週期
- Activity A 跳轉到 Activity B,生命週期的執行過程
- 擼擼Android的羊毛(二)----Activity生命週期Android
- Android 之 Activity 生命週期的淺析(二)Android
- Activity的生命週期和啟動模式詳解模式
- 文章之間的基本總結:Activity生命週期
- Android開發藝術(1)——Activity的生命週期和啟動模式Android模式
- 品牌生命週期和產品生命週期之間的關係
- Activity 生命週期,如何一手掌控
- Activity生命週期與啟動模式筆記模式筆記
- Android 之 Activity 生命週期淺析(一)Android
- 關於Laravel的二、三事(1)一次請求的生命週期Laravel
- Activity生命週期回撥是如何被回撥的?