android四大组件之Activity

android四大组件:Activity,Service,ContentProvider和BroadcastReceiver

 

首先,我们来深入了解下android应用中最为基本的一个组件——Activity

java.lang.Object
   ↳ android.content.Context
     ↳ android.content.ContextWrapper
       ↳ android.view.ContextThemeWrapper
         ↳ android.app.Activity

       对于开发者而言,Activity是android应用程序的入口,android应用程序模型没有定义想main()这样的入口方法,而是在Activity类中定义了一系列的生命周期方法,比如onCreate(),onDestroy()等方法。

       Activity类中的方法如下:

public class Activity extends ApplicationContext 
{
     protected void onCreate(Bundle savedInstanceState);
     protected void onStart();
     
     protected void onRestart();
     protected void onResume();
     protected void onPause();
     protected void onStop();
     protected void onDestroy();
 }

      

 

       可以看出,Activity类中定义了一系列的回调方法,以便对用户操作进行反应,而继续深入查看Activity类的相关信息,我们发现,Avtivity类实现了了Window.Callback,KeyEvent和ComponentCallbacks等多个接口,从而实现对事件的处理。Activity类中的方法的详细信息见下表:

 

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, oronStop() 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, oronStop() if it becomes invisible to the user.

YesonResume() 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, oronDestroy() 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 calledfinish() 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 theisFinishing() method.Yesnothing


 

       Activity呈现的用户界面是有View或ViewGroup构成的,因此Activity可以看做是View的载体,在Android系统中已经实现了很多实用的组件,比如之前博客中经常用到的Button、TextView、EditText等。

       View组件是android中所有UI控件、容器控件的基类,是用户实实在在看见的部分,但是View组件需要放到容器组件中,或者使用Activity将它显示出来。如果我们使用Eclipse新建一个android应用程序项目,我们会发现在Activity类中,已经为你写好了下面的代码:

protected void onCreate(Bundle savedInstanceState) 
{
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
}


       其中,setContentView()方法的作用便是把布局管理器(里面可能包含了若干Button、TextView等组件)显示出来。(如果程序不调用此函数,将会显示一个空窗口)

 

       了解了Activity的基本内容与作用后,我们来关注一下Activity的生命周期。

       Activity有4种状态:运行、暂停、停止和销毁

  • 运行(active/running):Activity位于屏幕前台,获得焦点(位于堆栈顶部)
  • 暂停(paused):Activity失去了焦点但仍可见。暂停的Activity仍然维持着成员信息和所状态,但当系统内存严重不足时可能被销毁
  • 停止(stopped):Activity完全被其它Activity覆盖。处于停止状态的Activity依然维持成员信息和所有状态,只是变得不可见了。当其它模块需要内存时,停止的Activity可能被销毁
  • 销毁:当Activity处于暂停或停止状态时,系统可能要求它结束生命周期或者直接杀死它所在的进程,从而从内存中删除它,此时的Activity就被销毁了

更清晰详细的解释见下面英文:

  • If an activity in the foreground of the screen (at the top of the stack), it isactive or running.
  • If an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it ispaused. A paused activity is completely alive (it maintains all state and member information and remains attached to the window manager), but can be killed by the system in extreme low memory situations.
  • If an activity is completely obscured by another activity, it isstopped. It still retains all state and member information, however, it is no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere.
  • If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state.

android生命周期图:

 

       从上图中我们看出

  • Activity整个生命周期始于onCreate()方法而至于onDestroy()方法。通常在onCreate()方法中构建Activity所需的资源(如调用setContentView()方法),并在onDestroy()方法中释放资源。
  • Activity的可视化生命周期始于onStart()方法而止于onStop()方法,此时的Activity是可见的,但可能无法和用户进行交互操作。在onStart()方法中可以注册BroadcastReceiver并且在onStop()方法中注销BroadcastReceiver。
  • Activity的前台生命周期始于onResume()方法而止于onPause()方法,此时Activity是可见的,位于堆栈的顶部。通常,需要在这两个方法里面处理外部事件,比如电话呼入。当电话呼入时,系统的Phone应用程序会进入前台,而当前运行的Activity则被覆盖。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值