第三步:Activity的生命周期

本文详细介绍了Android中Activity的生命周期,包括各个回调方法的作用、调用时机及其之间的转换关系。此外,还提到了如何通过合理地处理这些回调方法来提升用户体验。

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

官网文档是这样的:

MethodDescriptionKillable?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, etc.  This method also provides you with a Bundle containing the activity's previously frozen state, if there was one.   

Always followed by onStart().

NoonStart()
 onRestart()Called after your activity has been stopped, prior to it being started again.            

Always followed by onStart()

NoonStart()
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.

NoonResume() 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().

NoonPause()
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.

Pre-HONEYCOMBonResume() or
onStop()
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,

or onDestroy() if this activity is going away.

YesonRestart() or
onDestroy()
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.Yesnothing

 

onCreate()://一般先初始控件、 绑定监听器、绑使用布局文件XML

当一个新的Activity创建时自动调用该函数。

 

onRestart()://假如Activity已经被销毁,则不会调用该函数,会调用onCreate()

从一个Activity跳到另外一个Activity时,假设该Activity还没有被销毁,则会调用该函数。

 

onStart():

当Activity能被我们看到的时候就会调用。

 

onResume():

当获得用户焦点的时候就会调用。

 

onPause()://应该保存该Activity的数据,以便返回的时候重新绘制该Activity

当一个Activity启动另外一个Activity时就会调用该Activity的onPause()。

 

onStop()://若弹出的是对话框,对话框不能完全把该Activity遮挡住,故不会调用该Activity的OnStop()

当该Activity完全被遮挡住的时候就会调用该Activity的onStop()。

 

onDestroy();//类似析构函数

当Activity被销毁的时候会自动调用,一般由系统自动回收,也可以手动调用,调用方式是在Activity中调用finish()

 

 

android中Activity是采用栈的形式来储存的。

在开发过程中要注重用户体验,比如应用程序时用户调用了拨号程序,要注意保存相关数据,以便当用户按返回键的时候能够重新绘制出该应用程序的原貌。让用户觉得拨号程序跟应用程序时同一个程序。

就像栈一样,拨号程序入栈,当出栈后要能看到之前的内容。

用户所能看到的Activity都是位于栈顶的Activity。

 

关于对话框:

android的对话框并不像其他吗,面向对象的MessageBox那么方便,在android中,对话框,也是一个Activity,故该对话框同样需要独立的XML(不是必须)、同样需要在

AndroidManifest.xml配置文件中添加一个Activity

然后再Activity结点中添加一个属性:

android:theme="@android:style/Theme.Dialog"

让该Activity的显示方式为对话框风格就行了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值