Android Activity生命周期以及LoCat的使用


Activity主要有三个状态:

当在屏幕前台时(位于当前任务堆栈的顶部),它是活跃或运行的状态。它就是相应用户操作的Activity。

当它失去焦点但仍然对用户可见时,它处于暂停状态。即:在它之上有另外一个Activity。这个Activity也许是透明的,或者未能完全遮蔽全屏,所以被暂停的Activity仍对用户可见。暂停的Activity仍然是存活状态(它保留着所有的状态和成员信息并连接至窗口管理器),但当系统处于极低内存的情况下,仍然可以杀死这个Activity。

如果它完全被另一个Activity覆盖时,它处于停止状态。它仍然保留所有的状态和成员信息。然而它不再为用户可见,所以它的窗口将被隐藏,如果其他地方需要内存,则系统经常会杀死这个Activity。

API里Activity的生命周期图:

 

 Activity继承了ApplicationContext这个类,Activity的生命周期由下面七个方法实现:

 

  1. public class Activity extends ApplicationContext {   
  2.        protected void onCreate(Bundle savedInstanceState);   
  3.           
  4.        protected void onStart();      
  5.           
  6.        protected void onRestart();   
  7.           
  8.        protected void onResume();   
  9.           
  10.        protected void onPause();   
  11.           
  12.        protected void onStop();   
  13.           
  14.        protected void onDestroy();   
  15.    }  

   在学习Activity的生命周期时,为了更好理解Activity的生命周期,我基本重写了这七个方法,并用 Android.util.log这个类的静态方法打印出来 这个类相当的简单易用,因为它提供的全是一些静态方法:

  1. Log.v(String tag, String msg); //VERBOSE     
  2. Log.d(String tag, String msg); //DEBUG      
  3. Log.i(String tag, String msg); //INFO     
  4. Log.w(String tag, String msg); //WARN     
  5. Log.e(String tag, String msg); //ERROR   

下面是重写后的两个Activity的代码清单:FirstActivity

  1. package blog.activity.demo;   
  2. import android.app.Activity;   
  3. import android.content.Intent;   
  4. import android.os.Bundle;   
  5. import android.util.Log;   
  6. import android.view.View;   
  7. import android.view.View.OnClickListener;   
  8. import android.widget.Button;   
  9. public class FirstActivity extends Activity {   
  10.        
  11.     private final static String TAG = "FIRSTACITVITY";    
  12.     private Button myButton;   
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {   
  15.         Log.v(TAG, "FirstAcvity ---> onCreate");   
  16.         super.onCreate(savedInstanceState);   
  17.         setContentView(R.layout.main);   
  18.         myButton = (Button)findViewById(R.id.myButton);   
  19.         myButton.setText("启动第二个Activity");   
  20.         myButton.setOnClickListener(new ButtonOnClickListener());   
  21.     }   
  22.     @Override  
  23.     protected void onDestroy() {   
  24.         // TODO Auto-generated method stub   
  25.         Log.v(TAG, "FirstAcvity --->onDestory");   
  26.         super.onDestroy();   
  27.     }   
  28.     @Override  
  29.     protected void onPause() {   
  30.         // TODO Auto-generated method stub\   
  31.         Log.v(TAG, "FirstAcvity --->onPause");   
  32.         super.onPause();   
  33.     }   
  34.     @Override  
  35.     protected void onRestart() {   
  36.         // TODO Auto-generated method stub   
  37.         Log.v(TAG, "FirstAcvity --->onRestart");   
  38.         super.onRestart();   
  39.     }   
  40.     @Override  
  41.     protected void onResume() {   
  42.         // TODO Auto-generated method stub   
  43.         Log.v(TAG, "FirstAcvity --->onResume");   
  44.         super.onResume();   
  45.     }   
  46.     @Override  
  47.     protected void onStart() {   
  48.         // TODO Auto-generated method stub   
  49.         Log.v(TAG, "FirstAcvity --->onStart");   
  50.         super.onStart();   
  51.     }   
  52.     @Override  
  53.     protected void onStop() {   
  54.         // TODO Auto-generated method stub   
  55.         Log.v(TAG, "FirstAcvity --->onStop");   
  56.         super.onStop();   
  57.     }   
  58.        
  59.     class ButtonOnClickListener implements OnClickListener{   
  60.         @Override  
  61.         public void onClick(View v) {   
  62.             Intent intent = new Intent();   
  63.             intent.setClass(FirstActivity.this,SecondActivity.class);   
  64.             FirstActivity.this.startActivity(intent);   
  65.         }   
  66.            
  67.     }   
  68.        
  69. }  
  • 1
  • 2
  • 下一页

相关内容