Activity 生命周期

来源:互联网 发布:淘宝怎么上一千零一夜 编辑:程序博客网 时间:2024/06/06 13:05

Activity生命周期概述

     Activity概念:

        Activity是Android组件中最基本也是最为常见用的四大组件(Activity,Service服务,Content Provider内容提供者,BroadcastReceiver广播接收             器)之一。
Activity是一个应用程序组件,提供一个屏幕,用户可以用来交互为了完成某项任务。
在一个android应用中,一个Activity通常就是一个单独的屏幕,它上面可以显示一些控件也可以监听并处理用户的事件做出响应。Activity之间通过                Intent进行通信。
当我们新建一个安卓项目并创建一个主Activity时,我们可以看到这个Activity里有且只有一个OnCreate函数
    public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.main);
         }

Activity生命周期:


那么Activity还有其他函数没有写出来么?当然,我们先看一下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().意思是当activity第一次被创建的时候调用,在这里应该进行数据的初始化等工作。此方法提供给你一        个包含activity先前的状态。紧接着OnCreate后面的经常是OnStart;
由此我们可以知道还存在着其他的函数,如OnStart,OnResume,OnPause,OnStop,OnRestart,  OnDestroy,Activity处于不同的状态时会调用不同函数。
启动主Activity时,函数调用过程:OnCreate()->OnStart()->OnResume();
假设通过点击按钮触发事件跳转到另一个SecondActivity时,主Activity先调用pause()暂停;
SecondActivity调用OnCreate()->OnStart()->OnResume()从而实现跳转,此时主Activity不可见,然后主               Activity调用OnStop()停止,当按返回键从SecondActivity返回到主Activity时,
secondActivity先调用pause()暂停,主Activity调用OnReStart()->OnStart()->OnResume(),secondActivity           调用OnStop(),OnDestroy(),此时回到了主Activity;
假设通过点击按钮触发事件跳转到另一个ThirdActivity(ThirdActivity通过在AndroidManifest.xml设置了            android:theme="@android:style/Theme.Dialog"使其呈现出对话框的形式,当主Activity跳转到此Activity时,主Activity依然可见),此时他们各自的调用如下:主Activity先调用pause()暂停,ThirdActivity调用OnCreate()->OnStart()->OnResume()从而实现跳转,此时主Activity可见,当按返回键从ThirdActivity返回到主Activity时,ThirdActivity先调用pause()暂停主Activity调用直接OnResume(),而不必OnReStart()->OnStart()->OnResume()因为它并没有被Stop,ThirdActivity  调用OnStop(),OnDestroy(),此时回到了主Activity;




Activity不同函数所起到的功能:

MethodDescriptionKillable?NextonCreate()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.

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, 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


PS:Activity跳转代码:
Intent it = new Intent();
it.setClass(MainActivity.this, SecondActivity.class);
    startActivity(it);
此跳转代码不传递任何数据,只负责跳转。


0 0