[Android学习笔记]:Activity生命周期1

  Activity作为Android的四大组件之一,而且也是最常用的组件,其作用是非常大的,有必要把它的生命周期好好掌握。

  Android API中提供的生命周期图:

  Activity是继承了ApplicationContext类,同时可以重写以下方法:

public class Activity extends ApplicationContext {
     protected void onCreate(Bundle savedInstanceState);

     protected void onStart();
     
     protected void onRestart();

     protected void onResume();

     protected void onPause();

     protected void onStop();

     protected void onDestroy();
 }

  在API中也简单的描述了每个方法的作用,接下来以一个Demo来监听下这几个方法是怎么执行的。

  新建了一个ActivtyLifecycle的项目,基于4.0,重写了这7个方法。代码如下:

package com.androidliefcycle;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {
    private static final String TAG = "ActivityLifecycle";  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.d(TAG, "MainActivity-----onCreate");
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Log.d(TAG, "MainActivity-----onDestroy");
    }


    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        Log.d(TAG, "MainActivity-----onPause");
    }


    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();
        Log.d(TAG, "MainActivity-----onRestart");
    }


    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        Log.d(TAG, "MainActivity-----onResume");
    }


    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        Log.d(TAG, "MainActivity-----onStart");
    }


    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        Log.d(TAG, "MainActivity-----onStop");
    }
    
}

  用Log来查看它的运行。当我们第一次启动这个应用程序时候,得到的Log如下:

  由此,可以得出,在第一次启动一个Activity的时候,Android操作系统会调用onCreate,onStart,onResume方法。

  那在这三个方法中,具体的是做那些工作的呢?API中是如下描述:

  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.

当Activity第一次被创建时,此时可以创建视图,绑定数据等。

  onStart():Called when the activity is becoming visible to the user.

当Activity被展示出,即被用户看到。

  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.

当Activity与用户交互时候,此时Activity在栈的最上层。

  接下来,在MainActivity中添加一个按钮,其作用于跳转到第二个Activity,在项目中新建第二个Activity,SecondActivity.java,同样的重写这7个方法,代码如下:

package com.androidliefcycle;

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

public class SecondActivity extends Activity {
    private static final String TAG = "ActivityLifecycle";  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        Log.d(TAG, "SecondActivity-----onCreate");
    }


    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Log.d(TAG, "SecondActivity-----onDestroy");
    }


    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        Log.d(TAG, "SecondActivity-----onPause");
    }


    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();
        Log.d(TAG, "SecondActivity-----onRestart");
    }


    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        Log.d(TAG, "SecondActivity-----onResume");
    }


    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        Log.d(TAG, "SecondActivity-----onStart");
    }


    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        Log.d(TAG, "SecondActivity-----onStop");
    }
    
}

  当我们重新运行程序时,点击监视Log如下:

   此时在之前的onResume之后,执行了MainActivity的onPause(),接着执行了SeondActivity的onCreate,onStart,onResume,最后执行了MainActivity的onStop()

  接下来看下onPause,onStop的作用,API描述如下:

  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.

当系统将要重新载入之前的Activity。就是说在SecondActivity在onCreate前调用MainActivity的onPause方法。

  onStop():

  那onPause到底有什么作用呢,比如当我们在输入一个表单数据时候,按HOME键回到主页,再次回到表单Activity,此时原先的表单中输入的数据是不存在的,这样用户体验是比较糟糕的,任何解决这个问题,就需要用上onStop了。

定义一个变量_txtValue来存储表单txtValue的值

在onPause()中给_txtValue赋值,在onRestart给txtValue赋值。代码如下:

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        Log.d(TAG, "MainActivity-----onPause");
        _txtValue=txtValue.getText().toString();
    }


    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();
        Log.d(TAG, "MainActivity-----onRestart");
        txtValue.setText(_txtValue);
    }
原文地址:https://www.cnblogs.com/Enno/p/2972693.html