您的位置:首页 > 其它

重温Activity的各种状态

2010-11-24 11:55 120 查看
姐混不下去了,姐自学APP

今天重新温习了Activity的各种状态,GoogleDocument上面的那张图很是经典的,我好好看了,今天试了之前一直不太确定的问题

|-protected void onCreate(Bundle savedInstanceState);

|----- protected void onStart();

|----------- protected void onResume();

|----------- protected void onPause();

|----- protected void onStop();

| protected void onRestart();

|-protected void onDestroy();

我用log打印出我们常见的几个操作会看到的log如下:

1、开启

OnCreate

onStart

onResume

2、在开启后按下back键,其实就是退出程序咯

onPause

onStop

onDestroy

3、在开启后按下home键,其实没有结束程序,仅仅是关掉画面而已哦

onPause

onStop

4、按下home之后,重新开启之前关闭的activity

onRestart

onStart

onResume

5、在开启后,点击某个按钮,开启一个大小小于这个activity的新activity,并且设定为Dialog Theme,即不完全遮盖掉前一个Activity

onPause

新activity的OnCreate

新activity的onStart

新activity的onResume

6、继步骤5之后,按back

新activity的onPause

原activity的onResume

7、继步骤6后,再按back

原activity的onPause

新activity的onStop

新activity的onDestroy

原activity的onStop

原activity的onDestroy

8、在开启后,点击某个按钮,开启一个大小大于等于这个activity的新activity,即完全遮盖掉前一个Activity

onPause

新activity的OnCreate

新activity的onStart

新activity的onResume

onStop

9、继步骤8之后,按下back

新activity的OnPause

OnRestart

OnStart

OnResume

新activity的OnStop

新activity的OnDestory

现在用自己的理解阐述这几个状态的含义:

第一对:onCreate和onDestroy——创建与销毁

onCreate:Activity产生,伴随着上面的TextView之类也会一起产生,有点构造的意思

当然GoogleDocument讲得更清楚了,摘抄下:

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

onDestroy: Activity销毁,伴随着上面的TextView之类也会一起销毁,所以我有看到网上有讨论说按下home之后,再重新打开之前的那个页面,textview里面的文字会消失,我个人认为是不会的,只有没有执行onDestory,应该都在,我在android1.5上试了,是这样的

第二对:onStart和onStop——是否能看见,appear和disappear

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.

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.

第三对:有没有获取到操作的焦点,或者叫控制权

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.

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

单独的一个:OnRestart

其实就是这个activity被stop之后,onStart之前就一定会调用的

Called after your activity has been stopped, prior to it being started again.

Always followed by
onStart()


注意,官方文档上说OnPause,OnStop,OnDestory都是可以被kill掉的

是指after that method returns the process hosting the activity may killed by the system at any time without another line of its code being executed

Because of this, you should use the
onPause()
method to write any persistent data (such as user edits) to storage.

补充:onSaveInstanceState会在OnPause之前就调用,然后其中保存的这个bundle,只会在这个activity被异常的kill掉了才会在onCreate(Bundle savedInstanceState)中的savedInstanceState被传进来,如果是正常启动,是不会有的,即savedInstanceState==null
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐