Android学习笔记-Activity生命周期(转自Hello Android)

本文详细介绍了Android应用程序的各种生命周期状态,包括启动、运行、暂停、停止等阶段,并解释了每个阶段对应的回调方法及其用途,帮助开发者更好地理解如何管理和响应这些状态变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

转载于:https://www.cnblogs.com/penglink/archive/2010/12/24/1915637.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值