2010年4月27日 星期二

實做/觀察 Android Activity的生命週期

在實做Activity的生命週期前,我們先做一些功課:

對於什麼是Activity Lifecycle可以參考網站上這篇--
"Activity的生命週期"
http://cheng-min-i-taiwan.blogspot.com/2010/04/activity.html

Activity的Lifecycle觀念如果沒有建立清楚的話,很有可能會寫出一些浪費記憶體的程式,如果對於Activity Lifecycle那張圖不懂的話,實做一下下面的程式你將發現Activity Lifecycle的觀念會變得清晰多了。

下面影片是Demo一個Activity在按下"Home鍵"及"返回鍵"所呈現的Activity狀態,兩者間的是有差別的喔。


看完後再對應到下面的圖就很請楚了。


HelloActivity.java內容如下:
-----------------------程式開始----------------------------
package idv.android.HelloActivity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class HelloActivity extends Activity {
private static final String LC = "Life Cycle";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(LC, "onCreate");
Toast.makeText(this, "onCreate", Toast.LENGTH_LONG).show();
}
protected void onStart() {
super.onStart();
Log.d(LC, "onStart");
Toast.makeText(this, "onStart", Toast.LENGTH_LONG).show();
}
protected void onRestart() {
super.onRestart();
Log.d(LC, "onRestart");
Toast.makeText(this, "onRestart", Toast.LENGTH_LONG).show();
}
protected void onResume() {
super.onResume();
Log.d(LC, "onResume");
Toast.makeText(this, "onResume", Toast.LENGTH_LONG).show();
}
protected void onPause() {
super.onPause();
Log.d(LC, "onPause");
Toast.makeText(this, "onPause", Toast.LENGTH_LONG).show();
}
protected void onStop() {
super.onStop();
Log.d(LC, "onStop");
Toast.makeText(this, "onStop", Toast.LENGTH_LONG).show();
}
protected void onDestroy() {
super.onDestroy();
Log.d(LC, "onDestroy");
Toast.makeText(this, "onDestroy", Toast.LENGTH_LONG).show();
}
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.d(LC, "onRestoreInstanceState");
Toast.makeText(this, "onRestoreInstanceState", Toast.LENGTH_LONG).show();
}
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.d(LC, "onSaveInstanceState");
Toast.makeText(this, "onSaveInstanceState", Toast.LENGTH_LONG).show();
}
}
-----------------------程式結束----------------------------

程式中使用的Debug方法可以參考:
"AndroidDebug"
http://code.google.com/p/androidbmi/wiki/AndroidDebug

下面網址是有關於Android下"Home鍵"及"返回鍵"差別。
http://developer.android.com/intl/zh-TW/guide/topics/fundamentals.html#acttask

下面網址作者有模擬出幾個Scenario說明Activity的生命週期:
http://blog.aztaru.com/2009/12/10/%E7%AD%86%E8%A8%98android-%E7%9A%84-activity-lifecycle/

3 則留言:

  1. 只要用心去觀察就是研究。

    回覆刪除
  2. 老師你好看了您這篇的教學,對我的學習非常有用,所以我自己改了一下這篇的程式,多加了Button按下的動作setListensers();以及宣告元件函數findViews();分別塞在onCreate,onStart,onRestart,onResume,onPause,onStop,onDestroy,onRestoreInstanceState,onSaveInstanceState,觀察程式的動作

    package gao.chih.chien.ActivityExpBmi;

    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;

    public class ActivityExpBmi extends Activity {
    private static final String TAG = "Bmi";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Log.d(TAG,"onCreate");
    Toast.makeText(this, "onCreate", Toast.LENGTH_SHORT).show();

    }

    protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
    Log.d(TAG,"onStart");
    Toast.makeText(this, "onStart", Toast.LENGTH_SHORT).show();
    findViews();
    }

    protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    Log.d(TAG,"onResume");
    Toast.makeText(this, "onResume", Toast.LENGTH_SHORT).show();
    setListensers();
    }

    protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    Log.d(TAG,"onPause");
    Toast.makeText(this, "onPause", Toast.LENGTH_SHORT).show();

    }

    protected void onStop() {
    // TODO Auto-generated method stub
    super.onStop();
    Log.d(TAG,"onStop");
    Toast.makeText(this, "onStop", Toast.LENGTH_SHORT).show();
    }

    protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    Log.d(TAG,"onDestroy");
    Toast.makeText(this, "onDestroy", Toast.LENGTH_SHORT).show();

    }

    protected void onRestart() {
    // TODO Auto-generated method stub
    super.onRestart();
    Log.d(TAG,"onRestart");
    Toast.makeText(this, "onRestart", Toast.LENGTH_SHORT).show();
    }


    protected void onRestoreInstanceState(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onRestoreInstanceState(savedInstanceState);
    Log.d(TAG,"onRestoreInstanceState");
    Toast.makeText(this, "onRestoreInstanceState", Toast.LENGTH_SHORT).show();
    }

    protected void onSaveInstanceState(Bundle outState) {
    // TODO Auto-generated method stub
    super.onSaveInstanceState(outState);
    Log.d(TAG,"onSaveInstanceState");
    Toast.makeText(this, "onSaveInstanceState", Toast.LENGTH_SHORT).show();

    }

    private Button button_calc;
    private TextView view_result;
    private void findViews(){
    //宣告一個"Button"透過"findViewById"方法從資源檔中取得對應的介面元件
    button_calc = (Button)findViewById(R.id.Button01);
    view_result = (TextView)findViewById(R.id.TextView01);
    }

    private void setListensers() {
    //此函數來至於android.widget.Button
    button_calc.setOnClickListener(calcBMI);
    }
    private Button.OnClickListener calcBMI = new Button.OnClickListener(){
    public void onClick (View v){
    view_result.setText("Hello");
    }
    };
    }

    回覆刪除
  3. 作者已經移除這則留言。

    回覆刪除