关于activity的生命周期一

anctivity的生命周期比较复杂,但是很好的理解其生命周期,对于我们设计性能优良的应用程序有很大的帮助。
下面是官方文档上的一幅比较经典的activity生命周期示意图:

关于activity的生命周期一_第1张图片
其中涉及到七个生命周期函数,下面摘自官方文档的介绍:
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生命周期函数,来打印各个过程)

点击(此处)折叠或打开

  1. package com.example.android02;

  2. import org.apache.commons.logging.Log;

  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.Button;
  9. import android.widget.TextView;

  10. public class MainActivity extends Activity {

  11.     @Override
  12.     protected void onCreate(Bundle savedInstanceState) {
  13.         super.onCreate(savedInstanceState);
  14.         setContentView(R.layout.activity_main);
  15.         //Log.i(TAG,"onCreat");
  16.         TextView text = (TextView)findViewById(R.id.text1);
  17.         text.setText(R.string.number1);
  18.         Button button = (Button)findViewById(R.id.one);
  19.         button.setText(R.string.change);
  20.         button.setOnClickListener(new wang());
  21.         
  22.     }
  23.     
  24.     class wang implements OnClickListener{
  25.         
  26.         public void onClick(View v){

  27.             Intent intent = new Intent();
  28.             intent.setClass(MainActivity.this,TwoActivity.class);
  29.             MainActivity.this.startActivity(intent);

  30.             }
  31.         
  32.         //public 
  33.         

  34.     }
  35.     
  36.     protected void onStart() {
  37.         super.onStart();
  38.          System.out.println("first activity --》 onstart");
  39.         // The activity is about to become visible.
  40.     }
  41.     @Override
  42.     protected void onResume() {
  43.         super.onResume();
  44.         System.out.println("first activity -->onresume");
  45.         // The activity has become visible (it is now "resumed").
  46.     }
  47.     @Override
  48.     protected void onPause() {
  49.         super.onPause();
  50.         System.out.println("first activity -->onpause");
  51.         // Another activity is taking focus (this activity is about to be "paused").
  52.     }
  53.     @Override
  54.     protected void onStop() {
  55.         super.onStop();
  56.         System.out.println("first activity -->onstop");
  57.         // The activity is no longer visible (it is now "stopped")
  58.     }
  59.      protected void onRestart(){
        super.onRestart();
        System.out.println("first activity -->restart");
        
        }
  60.     @Override
  61.     protected void onDestroy() {
  62.         super.onDestroy();
  63.         System.out.println("first activity -->ondestory");
  64.         // The activity is about to be destroyed.
  65.     }
  66. }

TwoActivity:

点击(此处)折叠或打开

  1. package com.example.android02;

  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.widget.Button;
  5. import android.widget.TextView;

  6. public class TwoActivity extends Activity{
  7.     
  8.     
  9.     @Override
  10.     protected void onCreate(Bundle savedInstanceState) {
  11.         super.onCreate(savedInstanceState);
  12.         setContentView(R.layout.two);
  13.         TextView text = (TextView)findViewById(R.id.text2);
  14.         text.setText(R.string.number2);
  15.         @SuppressWarnings("unused")
  16.         Button button = (Button)findViewById(R.string.button2);
  17.     }
  18.     
  19.     protected void onStart() {
  20.         super.onStart();
  21.          System.out.println("second activity --》 onstart");
  22.         // The activity is about to become visible.
  23.     }
  24.     @Override
  25.     protected void onResume() {
  26.         super.onResume();
  27.         System.out.println("second activity -->onresume");
  28.         // The activity has become visible (it is now "resumed").
  29.     }
  30.     @Override
  31.     protected void onPause() {
  32.         super.onPause();
  33.         System.out.println("second activity -->onpause");
  34.         // Another activity is taking focus (this activity is about to be "paused").
  35.     }
  36.     @Override
  37.     protected void onStop() {
  38.         super.onStop();
  39.         System.out.println("second activity -->onstop");
  40.         // The activity is no longer visible (it is now "stopped")
  41.     }
  42.      protected void onRestart(){
        super.onRestart();
        System.out.println("second activity -->restart");
        
        }
  43.     @Override
  44.     protected void onDestroy() {
  45.         super.onDestroy();
  46.         System.out.println("second activity -->ondestory");
  47.         // The activity is about to be destroyed.
  48.     }
  49. }



此时我们通过运行程序,通过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的生命周期一)