从今天开始,写博客。以后利用博客记录下自己的学习内容


公司Android开发人员走了,不得以需要学习下Android开发。刚开始看别人的代码,各种不懂,闲暇之余,恶补下Android基础知识。

Activity是什么,个人理解,是Android提供用于和用户交互的界面。

Activity的生命周期主要包括onCreate、onStart、onResume、onPause、onStop、onRestart、onDestory,关系见下图。


整个Activity生命周期流程,如上图所示,几个典型的场景如下:

当Activity开始时,将依次调用onCreate、onStart、onResume方法,执行完成后,显示Activity对应UI界面。

当Acitivity开始后,用户按Back键时,依次调用onPause,onStop,onDestory方法,生命周期结束。

当Activity开始后,用户按Home键时,或者调用了其他的application,一次调用onPause,onStop方法。

public class ExampleActivity extends Activity {    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        // The activity is being created.    }    @Override    protected void onStart() {        super.onStart();        // The activity is about to become visible.    }    @Override    protected void onResume() {        super.onResume();        // The activity has become visible (it is now "resumed").    }    @Override    protected void onPause() {        super.onPause();        // Another activity is taking focus (this activity is about to be "paused").    }    @Override    protected void onStop() {        super.onStop();        // The activity is no longer visible (it is now "stopped")    }    @Override    protected void onDestroy() {        super.onDestroy();        // The activity is about to be destroyed.    }}

对Activity生命周期中几个方法的理解:

1、onCreate VS onStart

官方文档中描述:

~~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().

~~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.

即onCreate是当Activity创建时调用,用户用来初始化UI中的Views、数据绑定等。

onStart方法实在Activity变为可见时调用。

onCreate方法只能调用一次,而onStart方法可以多次调用。

2、onStart VS onResume

onStart文档中指出,onStart执行之后,会执行onResume方法,那为何还要多次一举,给出onStart接口呢,搜到了几篇比较好的文章解释

http://stackoverflow.com/questions/4553605/difference-between-onstart-and-onresume

http://blog.csdn.net/zhao_3546/article/details/12843477





更多相关文章

  1. Android读取本地或者网络图片的方法
  2. android Service--服务 .
  3. 安卓开发过程中遇到的问题总结及解决方法
  4. Android(安卓)AsyncTask基础知识整理
  5. Android(安卓)LayoutInflater 详解
  6. 如何判断Activity是否在前台显示
  7. Android(安卓)webview数据获取 webview抓取
  8. Android系统APP安装流程
  9. Android(安卓)Ril库总结

随机推荐

  1. Android(安卓)中能够作为 Log 开关的一些
  2. Android(安卓)Studio Git .gitignore规则
  3. Android(安卓)基础—— 对Context的理解
  4. Android(安卓)压缩网络上的图片BitmapFac
  5. Android(安卓)NDK开发 Cmake环境调用 so
  6. Android(安卓)异步查询 AsyncQueryHandle
  7. [android] android开发中常用的Eclipse快
  8. Android(安卓)SDK Manager无法安装packag
  9. Android(安卓)Wifi驱动--底层
  10. Android——Fragment的静态注册和动态注