Life Cycles of the Rich and Famous
During its lifetime,each activity of an Android program can be in one
of several states,as shown in Figure 2.3,on the following page.You,
the developer,do not have control over what state your program is in.
That’s all managed by the system.However,you do get notified when
the state is about to change through the onXX()method calls.

You override these methods in your Activity class,and Android will call
them at the appropriate time:
?onCreate(Bundle):This is called when the activity first starts up.
You can use it to perform one-time initialization such as creating
the user interface.onCreate()takes one parameter that is either
null or some state information previously saved by the onSaveIn-
stanceState()method.
?onStart():This indicates the activity is about to be displayed to the
user.
?onResume():This is called when your activity can start interacting
with the user.This is a good place to start animations and music.

?onPause():This runs when the activity is about to go into the back-
ground,usually because another activity has been launched in
front of it.This is where you should save your program’s persis-
tent state,such as a database record being edited.
?onStop():This is called when your activity is no longer visible to
the user and it won’t be needed for a while.If memory is tight,
onStop()may never be called(the system may simply terminate
your process).
?onRestart():If this method is called,it indicates your activity is
being redisplayed to the user from a stopped state.
?onDestroy():This is called right before your activity is destroyed.If
memory is tight,onDestroy()may never be called(the system may
simply terminate your process).
?onSaveInstanceState(Bundle):Android will call this method to allow
the activity to save per-instance state,such as a cursor position
within a text field.Usually you won’t need to override it because
the default implementation saves the state for all your user inter-
face controls automatically.4
?onRestoreInstanceState(Bundle):This is called when the activity is
being reinitialized from a state previously saved by the onSave-
InstanceState()method.The default implementation restores the
state of your user interface.
Activities that are not running in the foreground may be stopped or
the Linux process that houses them may be killed at any time in order
to make room for new activities.This will be a common occurrence,
so it’s important that your application be designed from the beginning
with this in mind.In some cases,the onPause()method may be the last
method called in your activity,so that’s where you should save any data
you want to keep around for next time.

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. AndroidMenifest.xml中android:sharedUse
  2. android 相对布局属性说明
  3. Android(安卓)Activity和Intent机制学习
  4. Android单元测试
  5. Android(安卓)搭建Junit测试环境
  6. Android下so注入汇总
  7. AndroidStudio Unknown attribute androi
  8. ProgressBar使用详解(进度条动画)
  9. 移动端H5的Video标签无法播放在线视频的
  10. AudioRecord||AudioTrack