Adnroid Activity生命周期。

转载:http://aina-hk55hk.iteye.com/blog/694315


1. 启动Activity:onCreate()->onStart()->onResume()->Activity is running

2. 按back键返回:onPause()->onStop()->onDestroy() 再次启动时:onCreate()->onStart()->onResume()->Activity is running

3.按home键返回:onPause()->onStop() 再次启动时:onRestart()->onStart()->onResume()->Activity is running

4.切换到别的Activity(当前Activity不finish): onPause()->onStop() 再次启动时:onCreate()->onStart()->onResume()->Activity is running


5.切换到别的Activity(当前Activity finish): onPause()->onStop()->onDestroy() 再次启动时:onCreate()->onStart()->onResume()->Activity is running



Java代码  收藏代码

   1. package com.Aina.Android; 
   2.  
   3. import android.app.Activity; 
   4. import android.content.Intent; 
   5. import android.os.Bundle; 
   6. import android.util.Log; 
   7. import android.view.View; 
   8. import android.widget.Button; 
   9.  
  10. public class Test_CeShi extends Activity { 
  11.     /** Called when the activity is first created. */ 
  12.     private int i = 1; 
  13.     private Button btn; 
  14.     /**
  15.      * 创建
  16.      */ 
  17.     @Override 
  18.     public void onCreate(Bundle savedInstanceState) { 
  19.         Log.i("TAG-onCreate", "onCreate()------------" + (i++)); 
  20.         super.onCreate(savedInstanceState); 
  21.         setContentView(R.layout.main); 
  22.         btn = (Button) this.findViewById(R.id.Button01); 
  23.         btn.setOnClickListener(new Button.OnClickListener(){ 
  24.  
  25.             @Override 
  26.             public void onClick(View v) { 
  27.                 Intent intent = new Intent(Test_CeShi.this,fg.class); 
  28.                 Test_CeShi.this.startActivity(intent); 
  29. //              Test_CeShi.this.finish(); 
  30.             } 
  31.              
  32.         }); 
  33.     } 
  34.  
  35.     /**
  36.      * 重新开始
  37.      */ 
  38.     @Override 
  39.     protected void onRestart() { 
  40.         Log.i("TAG-onRestart", "onRestart()------------" + (i++)); 
  41.         super.onRestart(); 
  42.     } 
  43.  
  44.     /**
  45.      * 启动
  46.      */ 
  47.     @Override 
  48.     protected void onStart() { 
  49.         Log.i("TAG-onStart", "onStart()------------" + (i++)); 
  50.  
  51.         super.onStart(); 
  52.     } 
  53.  
  54.     /**
  55.      * 重新启动
  56.      */ 
  57.     @Override 
  58.     protected void onResume() { 
  59.         Log.i("TAG-onResume", "onResume()------------" + (i++)); 
  60.         super.onResume(); 
  61.     } 
  62.  
  63.     /**
  64.      * 暂停
  65.      */ 
  66.     @Override 
  67.     protected void onPause() { 
  68.         Log.i("TAG-onPause", "onPause()------------" + (i++)); 
  69.  
  70.         super.onPause(); 
  71.     } 
  72.  
  73.     /**
  74.      * 停止
  75.      */ 
  76.     @Override 
  77.     protected void onStop() { 
  78.         Log.i("TAG-onStop", "onStop()------------" + (i++)); 
  79.         super.onStop(); 
  80.     } 
  81.  
  82.     /**
  83.      * 销毁
  84.      */ 
  85.     @Override 
  86.     protected void onDestroy() { 
  87.         Log.i("TAG-onDestroy", "onDestroy()------------" + (i++)); 
  88.         super.onDestroy(); 
  89.     } 
  90.  
  91. } 

你可能感兴趣的:(android,OS,Blog)