关于activity的生命周期1
下面是官方文档上的一幅比较经典的activity生命周期示意图:
其中涉及到七个生命周期函数,下面摘自官方文档的介绍:
| 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(). | No | onStart() | |
| onRestart() | Called after your activity has been stopped, prior to it being started again. Always followed by onStart() | No | 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. | No | onResume()or onStop() | |
| onResume() | Called when the activity will start interacting with the user. At this point your activity is at the top of the activity stack, with user input going to it. Always followed by onPause(). | No | onPause() |
| Called when the system is about to start resuming a previous activity. This is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, etc. Implementations of this method must be very quick because the next activity will not be resumed until this method returns. Followed by either onResume() if the activity returns back to the front, or onStop() if it becomes invisible to the user. | Yes | onResume()or onStop() | |
| Called when the activity is no longer visible to the user, because another activity has been resumed and is covering this one. This may happen either because a new activity is being started, an existing one is being brought in front of this one, or this one is being destroyed. Followed by either onRestart() if this activity is coming back to interact with the user, oronDestroy() if this activity is going away. | Yes | onRestart()or onDestroy() | |
| The final call you receive before your activity is destroyed. This can happen 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. | Yes | nothing |
1.onCreate()
Activity首次被创建时被调用,用于设置activity的布局文件,绑定按钮监听器等一些静态操作
2.onStart()
Activity对用户可见时被调用
3.onResume()
Activity获得用户焦点,即用户可以操作activity时被调用
4.onPause()
应用程序启动了其他activity时被调用。一般用于当前activity中的数据
5.onStop()
activity不可见时被调用
6.onRestart()
已停止的activity重新启动被调用
7.onDestroy()
调用activity的finish()方法或者android系统资源不足的时候被调用
我们通过简单的实验来说明一下:
下面的实验是创建两个activity,跳转的过程中,通过activity生命周期函数来说明activity的生命过程。由于布局很简单,下面只给出截图:
activity_main.xml:
two.xml:
MainActivity:(下面通过重写activity生命周期函数,来打印各个过程)
点击(此处)折叠或打开
super.onRestart();
System.out.println("first activity -->restart");
}
TwoActivity:
点击(此处)折叠或打开
super.onRestart();
System.out.println("second activity -->restart");
}
此时我们通过运行程序,通过Logcat可以看出打印出的各个生命周期阶段:
启动后,日志输出:(第一个activity经历了oncreate,onstart,onresume,获得焦点)
点击按钮然后进入第二个activity:(第一个activity可以是可见,但是失去了焦点,第二个activity获得了焦点)
此时点击back键:(此时第二个activity会失去焦点。而第一个activity之前被stop,此时会onrestart,重新获得焦点。从上面的activity生命图解可以很清楚的看出整个过程。这方面涉及到activity和Task相关知识,task以栈的形式存在,先启动的activity会先入栈,最后启动的activity会放在栈顶。上面第二个activity进栈的时候,此时完全遮盖了第一个activity,所以导致了第一个activity被onstop,但是没有被销毁,这也是当第二个activity出栈,失去焦点后,第一个activity restart,而没有oncreate的原因)
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29876893/viewspace-1815730/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/29876893/viewspace-1815730/
总结
以上是生活随笔为你收集整理的关于activity的生命周期1的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 双月楼月饼荣获茂名在线月饼品鉴佳绩
- 下一篇: Latex输入分段函数