生命周期图解
 
 
以下英文引用全部来自google官方文档说明,方便理解。

onCreate (Bundle savedInstanceState)

You can call finish() from within this function, in which case onDestroy() will be immediately called without any of the rest of the activity lifecycle (onStart()onResume()onPause(), etc) executing.
如果onCreate里面直接finish(),就不会走其他方法,会直接onDestory()。
可以自己打印日志试一下,确实如此。
 

onResume ()

Called after onRestoreInstanceState(Bundle)onRestart(), or onPause(), for your activity to start interacting with the user. This is a good place to begin animations, open exclusive-access devices (such as the camera), etc.
 
有可能在这三个方法之后调用,这是一个开启动画或者专有设备(比如Camera)的好时机。
 
Keep in mind that onResume is not the best indicator that your activity is visible to the user; a system window such as the keyguard may be in front. Use onWindowFocusChanged(boolean) to know for certain that your activity is visible to the user (for example, to resume a game).
 
但是它并不意味着你的Acitivity就对用户可见了,有可能系统的window还在你的前面,比如被锁屏的情况下这个acitivity已经OnResume,但是其实不可见。如果想确定是否这个avitivity对用户可见的,可以用onWindowFocusChanged(boolean)方法来确定。它会在onResume后运行。
 
例子:acitvity进入焦点
MainActivity onCreate
 MainActivity onStart
MainActivity onResume
 MainActivity onWindowFocusChanged  hasFocus=true
 
当此activity退出时
 MainActivity onWindowFocusChanged  hasFocus=false
 MainActivity onPause
 MainActivity onStop
 MainActivity onDestroy

onPause ()

Called as part of the activity lifecycle when an activity is going into the background, but has not (yet) been killed.

When activity B is launched in front of activity A, this callback will be invoked on A. B will not be created until A's onPause() returns, so be sure to not do anything lengthy here.

当Activity要进入后台时触发此方法,但是还没有被杀。

比如当B在A上面启动后,会触发此方法。除非A的onPause方法执行完,否则B不会create.所以要保证不在这个方法内做过多的事情。
 
This callback is mostly used for saving any persistent state the activity is editing, to present a "edit in place" model to the user and making sure nothing is lost if there are not enough resources to start the new activity without first killing this one. This is also a good place to do things like stop animations and other things that consume a noticeable amount of CPU in order to make the switch to the next activity as fast as possible, or to close resources that are exclusive access such as the camera.
 
这个方法主要用来保存一些Acitivity正在编辑的状态或者是关闭一些动画等事件让cpu更好运转,以便更迅速的开启下一个Activity(UI总是要先展示给用户看----生命周期设计精髓),或者是关闭一些资源,比如Camera.
 
After receiving this call you will usually receive a following call to onStop() (after the next activity has been resumed and displayed), however in some cases there will be a direct call back to onResume() without going through the stopped state.
 
在这个方法调用后会接着调用onStop,但是要等到下一个Activity被展示出来。


onStop ()

Called when you are no longer visible to the user.当对用户完全不可见的时候会调用此方法。

Note that this method may never be called, in low memory situations where the system does not have enough memory to keep your activity's process running after its onPause() method is called.

注意这个方法也许就根本不会被调用,当内存严重不足的时候Activity OnPause()后,已经没有足够内存就来执行OnStop();(如开始那幅图,直接onPause走左边)

其他注意事项:
从MainAcitivity----跳转--->SecondActivity发生的日志:
 
MainAcitivity onCreate()
MainAcitivity onStart()
MainAcitivity onResume()
MainAcitivity onPause()
                                                   SecondActivity onCreate()
                 SecondActivity onStart()
                 SecondActivity onResume()
 MainAcitivity onStop()

Android生命周期注意事项的更多相关文章

  1. android 生命周期

    引入android生命周期概念来做前端的hash切换页面的思路: 1. page manager来管理多个page(一对多的关系); 2. route:URL <-> Page; 3. P ...

  2. Android生命周期和Service生命周期

    android生命周期 运行:oncreate → onstart → onresume暂停:onresume → onpause:再次运行:onresume停止:onpause → onstop → ...

  3. Android生命周期里你也许不知道的事

    Android生命周期预计连刚開始学习的人都再熟悉只是的东西了,但这里我抛出几个问题.也许大家曾经没有想过或者可能认识的有些错误. 一.当A启动B时,A和B生命周期方法运行的先后顺序是如何的?当按返回 ...

  4. Android生命周期

    Android的生命周期如下图所示: A和B两个Activity,从A启动B活动.执行的方法: A活动 onCreate() onStart() onResume()                 ...

  5. Android生命周期详细说明

    提供两个关于Activity的生命周期模型图示帮助理解:                                           图1 图2 从图2所示的Activity生命周期不难看出, ...

  6. Android 生命周期 和 onWindowFocusChanged

    转载 http://blog.csdn.net/pi9nc/article/details/9237031 onWindowFocusChanged重要作用 Activity生命周期中,onStart ...

  7. Android生命周期回顾

    先回顾生命周期 Activity一共有3中状态 运行中 Activity位于前台,并具有用户焦点 暂停 另一个Activity位于屏幕前台并具有用户焦点,但此Activity仍可见.也就是说,另一个A ...

  8. Android生命周期例子小解

    Activity 从创建到进入运行态所触发的事件 onCreate()-->onStart-->onResume() 从运行态到停止态所触发的事件                 onPa ...

  9. Android 生命周期,解决savedInstanceState一直为null的问题

    在此之前还是补充下关于监听器的两个要点:   向下一个活动传递数据 : button1.setOnClickListener(new View.OnClickListener() { @Overrid ...

随机推荐

  1. C#基本语句与C++区别

    条件语句必须为bool表达式 int a = 1; if(a) { ... } 在c++中可以,但c#报错 但 bool b = true;//不能写成b = 1了: if(b) { ... } 是可 ...

  2. 洛谷 P3367 【模板】并查集

    P3367 [模板]并查集 题目描述 如题,现在有一个并查集,你需要完成合并和查询操作. 输入输出格式 输入格式: 第一行包含两个整数N.M,表示共有N个元素和M个操作. 接下来M行,每行包含三个整数 ...

  3. CODEVS 3137 栈练习1

    3137 栈练习1 时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题目描述 Description 给定一个栈(初始为空,元素类型为整数,且小于等于100),只 ...

  4. 医院设置 codevs 2577

    Floyd+二叉树 #include<iostream>#include<cstdlib>#include<cstdio>#include<cstring&g ...

  5. 数值运算内建函数(core python programming 2nd edition 5.6.2)

    数值运算内建函数 函数  功能 abs(num) 返回 num 的绝对值 coerce(num1, num2) 将num1和num2转换为同一类型,然后以一个元组的形式返回. divmod(num1, ...

  6. .net mvc结合微软提供的FormsAuthenticationTicket登陆

    一.Web.config <system.web> <compilation debug="true" targetFramework="4.5&quo ...

  7. Django-Rest-Framework 教程: 快速入门

    本篇中, 我们会创建一个简单的API, 用来查看和编辑django默认的user和group数据. 1. 设置 我们创建django项目tutorial, 和app quickstart: # 创建新 ...

  8. js实现选项卡切换的三种方式

    前两种主要实现一个选项卡的切换,第三种使用闭包看书,构造函数实现多个选项卡的切换: 1.第一种实现实现效果为: 实现代码为: <!doctype html> <!DOCTYPE ht ...

  9. 使用 JUnit 进行单元测试 - 教程

    tanyuanji@126.com 版本历史 JUnit 该教程主要讲解 JUnit 4.x 版本的使用,以及如何在Eclipse IDE 中如何使用JUnit   目录 tanyuanji@126. ...

  10. 下载Google浏览器(Google Chrome)离线安装包方法

    Chrome浏览器默认是在线安装的,但由于网络的原因,有时很久也不能完成安装.其实Chrome官方是提供离线安装包的.具体地址如下: 稳定版:http://www.google.com/chrome/ ...