Android中主要组件之Activity的生命周期,基本都是翻译Android API和个人的理解。

首先看一下Android api中所提供的Activity生命周期图:

Activity其实是继承了ApplicationContext这个类,我们可以重写以下方法,如下代码:

 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     }
View Code

下面是一个简单的Demo,可以加深理解Activity周期。

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

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

 1 package com.tutor.activitydemo; 2 import android.app.Activity; 3 import android.os.Bundle; 4 import android.util.Log; 5 public class ActivityDemo extends Activity { 6     7     private static final String TAG = "ActivityDemo"; 8      9     public void onCreate(Bundle savedInstanceState) {10         super.onCreate(savedInstanceState);11         setContentView(R.layout.main);12         13         Log.e(TAG, "start onCreate~~~");14     }15     16     @Override17     protected void onStart() {18         super.onStart();19         Log.e(TAG, "start onStart~~~");20     }21     22     @Override23     protected void onRestart() {24         super.onRestart();25         Log.e(TAG, "start onRestart~~~");26     }27     28     @Override29     protected void onResume() {30         super.onResume();31         Log.e(TAG, "start onResume~~~");32     }33     34     @Override35     protected void onPause() {36         super.onPause();37         Log.e(TAG, "start onPause~~~");38     }39     40     @Override41     protected void onStop() {42         super.onStop();43         Log.e(TAG, "start onStop~~~");44     }45     46     @Override47     protected void onDestroy() {48         super.onDestroy();49         Log.e(TAG, "start onDestroy~~~");50     }51     52 }
View Code

第三步:运行上述工程,效果图如下(没有任何异样):

问题主要显示在Logcat视窗里,我们打开应用时先后执行了onCreate()->onStart()->onResume三个方法,看一下LogCat视窗如下:

BACK键:

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

HOME键:

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

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

这里就会出现一个经常遇到想过的问题,当按HOME键,然后再进入ActivityDemo应用时,我们的应用的状态应该是和按HOME键之前的状态是一样的?在这里我将ActivityDemo的代码作一些修改,就是增加一个EditText。

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

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:orientation="vertical" 4     android:layout_width="fill_parent" 5     android:layout_height="fill_parent" 6     > 7 <TextView   8     android:layout_width="fill_parent"  9     android:layout_height="wrap_content" 10     android:text="@string/hello"11     />12 <EditText13     android:id="@+id/editText"14     android:layout_width="fill_parent"15     android:layout_height="wrap_content"16 />17 </LinearLayout>
View Code

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

这时候,大家可以按一下HOME键,然后再次启动ActivityDemo应用程序,这时候EditText里并没有我们输入的"Frankie"字样,如下图:

这显然不能称得一个合格的应用程序,所以需要在Activity几个方法里自己实现,如下第六步所示:

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

 1 package com.tutor.activitydemo; 2 import android.app.Activity; 3 import android.os.Bundle; 4 import android.util.Log; 5 import android.widget.EditText; 6 public class ActivityDemo extends Activity { 7     8     private static final String TAG = "ActivityDemo"; 9     private EditText mEditText;10     //定义一个String 类型用来存取我们EditText输入的值11     private String mString;12     public void onCreate(Bundle savedInstanceState) {13         super.onCreate(savedInstanceState);14         setContentView(R.layout.main);15         mEditText = (EditText)findViewById(R.id.editText);16         Log.e(TAG, "start onCreate~~~");17     }18     19     @Override20     protected void onStart() {21         super.onStart();22         Log.e(TAG, "start onStart~~~");23     }24     //当按HOME键时,然后再次启动应用时,我们要恢复先前状态25     @Override26     protected void onRestart() {27         super.onRestart();28         mEditText.setText(mString);29         Log.e(TAG, "start onRestart~~~");30     }31     32     @Override33     protected void onResume() {34         super.onResume();35         Log.e(TAG, "start onResume~~~");36     }37     38     //当我们按HOME键时,我在onPause方法里,将输入的值赋给mString39     @Override40     protected void onPause() {41         super.onPause();42         mString = mEditText.getText().toString();43         Log.e(TAG, "start onPause~~~");44     }45     46     @Override47     protected void onStop() {48         super.onStop();49         Log.e(TAG, "start onStop~~~");50     }51     52     @Override53     protected void onDestroy() {54         super.onDestroy();55         Log.e(TAG, "start onDestroy~~~");56     }57     58 }
View Code

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

OK,大功基本告成,这就是Activity的生命周期。

更多相关文章

  1. 4种必须知道的Android屏幕自适应解决方案
  2. Android(安卓)主动获取电量的方法
  3. Android(安卓)Studio的Gradle错误解决方法
  4. Android的系统架构
  5. android Bitmap
  6. SharedPreferences之Android数据保存
  7. Android(安卓)复盘——你真的了解 setContentView 吗?
  8. 浅谈Java中Collections.sort对List排序的两种方法
  9. Python list sort方法的具体使用

随机推荐

  1. 关于build-tools 26.0.0报错解决及Neon3
  2. android 弹出对话框
  3. Android的前世今生
  4. Android(安卓)核心分析 之六 -----IPC框
  5. android 笔记:判断手机是否显示虚拟按键
  6. android 教程
  7. android实现渐变效果
  8. Android记事本的开发
  9. Android NetworkLocationProvider and Ge
  10. Android手机开发:图片的放大和缩小显示Ima