2010年11月8日 星期一

[Android] Activity Lifecycles 生命週期




































執行App時: OnCreate() -> OnStart() -> OnResume()
結束App時: OnPause() -> OnStop() -> OnDestroy()
Finish() -> OnPause() -> OnStop() -> OnDestroy()
暫停App時: OnPause() -> OnStpop()
重啟App時: OnRestart() -> OnStart() -> OnResume()

以下是測試程式 (使用Toast顯示文字):

package com.demo.activitycycle;

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

public class ActivityLifeCycle extends Activity {

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
     Toast.makeText(this, "OnCreate()", Toast.LENGTH_SHORT).show();
 }

 @Override
 protected void onDestroy() {
  super.onDestroy();
  Toast.makeText(this, "OnDestroy()", Toast.LENGTH_SHORT).show();
 }

 @Override
 protected void onPause() {
  super.onPause();
  Toast.makeText(this, "OnPause()", Toast.LENGTH_SHORT).show();
 }

 @Override
 protected void onRestart() {
  super.onRestart();
  Toast.makeText(this, "OnRestart()", Toast.LENGTH_SHORT).show();
 }

 @Override
 protected void onResume() {
  super.onResume();
  Toast.makeText(this, "OnResume()", Toast.LENGTH_SHORT).show();
 }

 @Override
 protected void onStart() {
  super.onStart();
  Toast.makeText(this, "OnStart()", Toast.LENGTH_SHORT).show();
 }

 @Override
 protected void onStop() {
  super.onStop();
  Toast.makeText(this, "OnStop()", Toast.LENGTH_SHORT).show();
 }
}

Reference:
http://developer.android.com/guide/topics/fundamentals.html#lcycles
http://developer.android.com/reference/android/widget/Toast.html
Related Posts Plugin for WordPress, Blogger...