两分钟完全让你明白Android Activity生命周期(图文)

你们好,今天给你们详解一下Android中Activity的生命周期,我在前面也曾经讲过这方面的内容,可是像网上大多数文章同样,基本都是翻译Android API,过于笼统,相信你们看了,会有一点点的帮助,可是还不能彻底吃透,因此我今天特地在从新总结一下. java

  首先看一下Android api中所提供的Activity生命周期图(不明白的,能够看完整篇文章,在回头看一下这个图,你会明白的): android

Activity实际上是继承了ApplicationContext这个类,咱们能够重写如下方法,以下代码: api

 

view plaincopy to clipboardprint?
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();  
   }


  为了便于你们更好的理解,我简单的写了一个Demo,不明白Activity周期的朋友们,能够亲手实践一下,你们按照个人步骤来。 浏览器

  第一步:新建一个Android工程,我这里命名为ActivityDemo. app

  第二步:修改ActivityDemo.java(我这里从新写了以上的七种方法,主要用Log打印),代码以下:      ide

package com.tutor.activitydemo;  
import android.app.Activity;  
import android.os.Bundle;  
import android.util.Log;  
public class ActivityDemo extends Activity {  
    private static final String TAG = "ActivityDemo";  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        Log.e(TAG, "start onCreate~~~");  
    }  
    @Override  
    protected void onStart() {  
        super.onStart();  
        Log.e(TAG, "start onStart~~~");  
    }  
    @Override  
    protected void onRestart() {  
        super.onRestart();  
        Log.e(TAG, "start onRestart~~~");  
    }  
    @Override  
    protected void onResume() {  
        super.onResume();  
        Log.e(TAG, "start onResume~~~");  
    }  
    @Override  
    protected void onPause() {  
        super.onPause();  
        Log.e(TAG, "start onPause~~~");  
    }  
    @Override  
    protected void onStop() {  
        super.onStop();  
        Log.e(TAG, "start onStop~~~");  
    }  
    @Override  
    protected void onDestroy() {  
        super.onDestroy();  
        Log.e(TAG, "start onDestroy~~~");  
    }  
}

  第三步:运行上述工程,效果图以下(没什么特别的): 布局

  核心在Logcat视窗里,若是你还不会用Logcat你能够看一下个人这篇文章 Log图文详解(Log.v,Log.d,Log.i,Log.w,Log.e),咱们打开应用时前后执行了onCreate()->onStart()->onResume三个方法,看一下LogCat视窗以下: spa

  BACK键: .net

  当咱们按BACK键时,咱们这个应用程序将结束,这时候咱们将前后调用onPause()->onStop()->onDestory()三个方法,以下图所示: 翻译

  HOME键:

  当咱们打开应用程序时,好比浏览器,我正在浏览NBA新闻,看到一半时,我忽然想听歌,这时候咱们会选择按HOME键,而后去打开音乐应用程序,而当咱们按HOME的时候,Activity前后执行了onPause()->onStop()这两个方法,这时候应用程序并无销毁。以下图所示:

  而当咱们再次启动ActivityDemo应用程序时,则前后分别执行了onRestart()->onStart()->onResume()三个方法,以下图所示:

  这里咱们会引出一个问题,当咱们按HOME键,而后再进入ActivityDemo应用时,咱们的应用的状态应该是和按HOME键以前的状态是同样的,一样为了方便理解,在这里我将ActivityDemo的代码做一些修改,就是增长一个EditText。

  第四步:修改main.xml布局文件(增长了一个EditText),代码以下:

<?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
     android:orientation="vertical"  
     android:layout_width="fill_parent"  
     android:layout_height="fill_parent"  
     >  
 <TextView    
     android:layout_width="fill_parent"   
     android:layout_height="wrap_content"   
     android:text="@string/hello"  
     />  
 <EditText  
     android:id="@+id/editText"  
     android:layout_width="fill_parent"  
     android:layout_height="wrap_content"  
 />  
 </LinearLayout>

 

  第五步:而后其余不变,运行ActivityDemo程序,在EditText里输入如"Frankie"字符串(以下图:)

 

  这时候,你们能够按一下HOME键,而后再次启动ActivityDemo应用程序,这时候EditText里并无咱们输入的"Frankie"字样,以下图:

  这显然不能称得一个合格的应用程序,因此咱们须要在Activity几个方法里本身实现,以下第六步所示:

  第六步修改ActivityDemo.java代码以下:

package com.tutor.activitydemo;  
 import android.app.Activity;  
 import android.os.Bundle;  
 import android.util.Log;  
 import android.widget.EditText;  
 public class ActivityDemo extends Activity {  
     private static final String TAG = "ActivityDemo";  
     private EditText mEditText;  
     //定义一个String 类型用来存取咱们EditText输入的值  
     private String mString;  
     public void onCreate(Bundle savedInstanceState) {  
         super.onCreate(savedInstanceState);  
         setContentView(R.layout.main);  
         mEditText = (EditText)findViewById(R.id.editText);  
         Log.e(TAG, "start onCreate~~~");  
     }  
     @Override  
     protected void onStart() {  
         super.onStart();  
         Log.e(TAG, "start onStart~~~");  
     }  
     //当按HOME键时,而后再次启动应用时,咱们要恢复先前状态  
     @Override  
     protected void onRestart() {  
         super.onRestart();  
         mEditText.setText(mString);  
         Log.e(TAG, "start onRestart~~~");  
     }   
     @Override  
     protected void onResume() {  
         super.onResume();  
         Log.e(TAG, "start onResume~~~");  
     }  
     //当咱们按HOME键时,我在onPause方法里,将输入的值赋给mString  
     @Override  
     protected void onPause() {  
         super.onPause();  
         mString = mEditText.getText().toString();  
         Log.e(TAG, "start onPause~~~");  
     }   
     @Override  
     protected void onStop() {  
         super.onStop();  
         Log.e(TAG, "start onStop~~~");  
     }  
       
     @Override  
     protected void onDestroy() {  
         super.onDestroy();  
         Log.e(TAG, "start onDestroy~~~");  
     }  
 }

 

  第七步:从新运行ActivityDemo程序,重复第五步操做,当咱们按HOME键时,再次启动应用程序时,EditText里有上次输入的"Frankie"字样,以下图如示:

  OK,大功基本告成,这时候你们能够在回上面看一下Activity生命周期图,我想你们应该彻底了解了Activity的生命周期了,不知道你了解了没?

相关文章
相关标签/搜索