您的位置:首页 > 移动开发 > Android开发

android开发艺术探索学习 之 Activity的生命周期

2016-02-25 14:07 746 查看
转载请标明出处:
/article/3728074.html
本文出自:【Smile的博客】

欢迎评论吐槽拍砖



首先看这些方法这什么时候调用。

官方文档是这样描述的:

MethodDescriptionKillable after?Next
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, and so on. This method is passed a Bundle object containing the activity's previous state, if that state was captured
(see Saving Activity State, later).
Always followed by
onStart()
.

No
onStart()
onRestart()
Called after the activity has been stopped, just prior to it being started again.
Always followed by
onStart()

No
onStart()
onStart()
Called just before the activity becomes visible to the user.
Followed by
onResume()
if the activity comes to the foreground, or
onStop()
if it becomes hidden.
No
onResume()


or

onStop()
onResume()
Called just before the activity starts interacting with the user. At this point the activity is at the top of the activity stack, with user input going to it.
Always followed by
onPause()
.
No
onPause()
onPause()
Called when the system is about to start resuming another activity. This method is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, and so on. It should do whatever it does very quickly,
because the next activity will not be resumed until it returns.
Followed either by
onResume()
if the activity returns back to the front, or by
onStop()
if it becomes invisible to the user.
Yes
onResume()


or

onStop()
onStop()
Called when the activity is no longer visible to the user. This may happen because it is being destroyed, or because another activity (either an existing one or a new one) has been resumed and is covering it.
Followed either by
onRestart()
if the activity is coming back to interact with the user, or by
onDestroy()
if this activity is going away.
Yes
onRestart()


or

onDestroy()
onDestroy()
Called before the activity is destroyed. This is the final call that the activity will receive. It could be called either because the activity is finishing (someone called
finish()
on
it), or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the
isFinishing()
method.
Yesnothing

大致的中文描述是

当第一次调用一个Activity就会执行onCreate方法.后面总是接着执行onStart方法.

当Activity处于可见状态的时候就会调用onStart方法.接着如果调用onResume我们就会看到这个界面,调用onStop方法的话就会被隐藏。

当Activity可以得到用户焦点的时候就会调用onResume方法.后面总是调用onPause方法.

当Activity没有被销毁的时候重新调用这个Activity就会调用onRestart方法.后面总是接着执行onStart方法.

当系统将要开始加载另外一个Activity的时候调用onPause方法.这个方法通常用于提交固定的数据、停止动画和其他可能消耗CPU的事情。onPause必须运行的很快,因为将要加载的Activity在它返回的时候才会“恢复”。如果重新返回这个Activity执行onResume方法,如果这个Activity不可见的时候就调用onStop方法.

当Activity处于不可见状态的时候就会调用onStop方法.在Activity将要被销毁的时候 或者 一个新的Activity出现在界面的时候调用. 接下来条用onRestart或者onDestory方法..

当Activity被销毁时会调用onDestory方法.

有两个测试activity。1代表 MainActivity,2代表TestActivity

测试机:genymotion模拟器Google Nexus 4 - 4.4.4 API 19 768*1280 (320dpi)

1.首先是MainActivity正常启动,logcat信息如下:

02-24 16:08:51.417 10924-10924/xiaoke.hnpolice.com.activitylifecircle E/----1----: onCreate()

02-24 16:08:51.461 10924-10924/xiaoke.hnpolice.com.activitylifecircle E/----1----: onStart()

02-24 16:08:51.469 10924-10924/xiaoke.hnpolice.com.activitylifecircle E/----1----: onResume()

2.点击返回,logcat信息如下:

02-24 16:16:06.244 14998-14998/xiaoke.hnpolice.com.activitylifecircle E/----1----: onPause()

02-24 16:16:07.144 14998-14998/xiaoke.hnpolice.com.activitylifecircle E/----1----: onStop()

02-24 16:16:07.144 14998-14998/xiaoke.hnpolice.com.activitylifecircle E/----1----: onDestroy()

3.启动MainActivity,然后点击跳转到TestActivity,logcat信息如下:

02-24 16:18:47.018 17426-17426/xiaoke.hnpolice.com.activitylifecircle E/----1----: onCreate()

02-24 16:18:47.046 17426-17426/xiaoke.hnpolice.com.activitylifecircle E/----1----: onStart()

02-24 16:18:47.054 17426-17426/xiaoke.hnpolice.com.activitylifecircle E/----1----: onResume()

02-24 16:18:56.262 17426-17426/xiaoke.hnpolice.com.activitylifecircle E/----1----: onPause()

02-24 16:18:56.278 17426-17426/xiaoke.hnpolice.com.activitylifecircle E/----2----: onCreate()

02-24 16:18:56.294 17426-17426/xiaoke.hnpolice.com.activitylifecircle E/----2----: onStart()

02-24 16:18:56.294 17426-17426/xiaoke.hnpolice.com.activitylifecircle E/----2----: onResume()

02-24 16:18:57.162 17426-17426/xiaoke.hnpolice.com.activitylifecircleE/----1----: onStop()

我们可以看到TestActivity的创建 是在MainActivity的onPause()之后,onStop之前.所以当有面试官问的时候,你就知道怎么回答了吧。

4.启动MainActivity,然后旋转屏幕,,logcat信息如下:

02-24 16:42:39.068 5740-5740/xiaoke.hnpolice.com.activitylifecircle E/----1----: onCreate()

02-24 16:42:39.112 5740-5740/xiaoke.hnpolice.com.activitylifecircle E/----1----: onStart()

02-24 16:42:39.112 5740-5740/xiaoke.hnpolice.com.activitylifecircle E/----1----: onResume()

02-24 16:42:50.632 5740-5740/xiaoke.hnpolice.com.activitylifecircle E/----1----: onPause()

02-24 16:42:50.640 5740-5740/xiaoke.hnpolice.com.activitylifecircle E/----1----: onStop()

02-24 16:42:50.640 5740-5740/xiaoke.hnpolice.com.activitylifecircle E/----1----: onDestroy()

02-24 16:42:50.720 5740-5740/xiaoke.hnpolice.com.activitylifecircle E/----1----: onCreate()

02-24 16:42:50.728 5740-5740/xiaoke.hnpolice.com.activitylifecircle E/----1----: onStart()

02-24 16:42:50.728 5740-5740/xiaoke.hnpolice.com.activitylifecircle E/----1----: onResume()

我们可以看到 MainActivity在进行旋转屏幕操作的时候,先销毁了自己,然后再重新创建。所以你懂的,面试官问的时候你就知道该怎么回答了。

5.我们在onCreate()中出现加上下面这句之后,logcat信息如下。

List<String> s = null;
int size = s.size();

02-24 17:20:56.291 3835-3835/xiaoke.hnpolice.com.activitylifecircle E/----1----: onCreate()
02-24 17:20:56.335 3835-3835/xiaoke.hnpolice.com.activitylifecircle E/AndroidRuntime: FATAL EXCEPTION: main Process: xiaoke.hnpolice.com.activitylifecircle,

PID: 3835 java.lang.RuntimeException: Unable to start activity ComponentInfo{xiaoke.hnpolice.com.activitylifecircle/xiaoke.hnpolice.com.activitylifecircle.MainActivity}: java.lang.NullPointerException ...

Caused by: java.lang.NullPointerException at xiaoke.hnpolice.com.activitylifecircle.MainActivity.onCreate(MainActivity.java:30)...


当在onCreate()中出现异常时.MainActivity只会调用onCreate()方法。

测试项目地址: https://github.com/103style/ActivityLifeCircle
友情链接 : 如何托管你的项目到github上详细教程
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: