Activities
概念
一个activity是android提供的一个应用组件,提供一块屏幕用于用户进行交互。像打电话,发短信,看地图。每个activity提供了一个窗口视图用于绘制用户界面,窗口可能和屏幕一样大,可能比屏幕小,也可能在其他窗口之上。
生命周期
一个activity本质上存在三种状态:
Resumed
The activity is in the foreground of the screen and has user focus. (This state is also sometimes referred to as "running".)
Paused
Another activity is in the foreground and has focus, but this one is still visible. That is, another activity is visible on top of this one and that activity is partially transparent or doesn't cover the entire screen. A paused activity is completely alive (the object is retained in memory, it maintains all state and member information, and remains attached to the window manager), but can be killed by the system in extremely low memory situations.
Stopped
The activity is completely obscured by another activity (the activity is now in the "background"). A stopped activity is also still alive (the object is retained in memory, it maintains all state and member information, but is
not attached to the window manager). However, it is no longer visible to the user and it can be killed by the system when memory is needed elsewhere.
保存Activity状态
一句话,当Activity被调用onDestory的时候,activity的一切都被销毁,此时需要实现onSaveInstanceState() callback来保存activiy的状态信息。
系统会在activity被销毁之前调用onSaveInstanceState(),系统调用此函数时,会传递一个bundle过来,可以用类似putString和putInt的方法存放一些键值对,当activity被重新创建,调用onCreate和onRestoreInstanceState()的时候,会将此bundle传递到函数中,这样就可以获取销毁前的保存信息了。如果没有信息,bundle会是null。
文档中还提到,此方法不保证会在Activity销毁的时候被调用,所以一般此方法应该只是用来记录activity的转换状态,不建议存放数据。
Activity A start Activity B 时序流程
阅读(2127) | 评论(0) | 转发(0) |