Chinaunix首页 | 论坛 | 博客
  • 博客访问: 869227
  • 博文数量: 322
  • 博客积分: 6688
  • 博客等级: 准将
  • 技术积分: 3626
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-19 11:26
文章分类

全部博文(322)

文章存档

2013年(5)

2012年(66)

2011年(87)

2010年(164)

分类: Java

2011-01-07 09:27:52

生命周期

LifeActivity

Java代码

  1. package org.wp.activity;
  2.  
  3. /**
  4.  *
  5.  * Activity生命周期
  6.  *
  7.  * Activity有三个状态:
  8.  * 当它在屏幕前台时(位于当前任务堆栈的顶部),它是激活或运行状态
  9.  * 它就是响应用户操作的Activity
  10.  *
  11.  * 当它失去焦点但仍然对用户可见时
  12.  * 它处于暂停状态
  13.  * 即在它之上有另外一个Activity,这个Activity也许是透明的,或者没有完全覆盖全屏
  14.  * 所以被暂停的Activity仍对用户可见
  15.  * 暂停的Activity仍然是存活状态(它保留着所有的状态和成员信息并保持和窗口管理器的连接)
  16.  * 但系统处于极低内存时仍然可以杀死这个Activity
  17.  *
  18.  * 完全被另一个Activity覆盖时则处于停止状态
  19.  * 它仍然保留所有的状态和成员信息
  20.  * 然而对用户是不可见的,所以它的窗口将被隐藏
  21.  * 如果其它地方需要内存,则系统经常会杀死这个Activity
  22.  *
  23.  * 当Activity从一种状态转变到另一种状态时,会调用以下保护方法来通知这种变化:
  24.  * public void onCreate(Bundle savedInstanceState)
  25.  * protected void onStart()
  26.  * protected void onResume()
  27.  * protected void onRestart()
  28.  * protected void onPause()
  29.  * protected void onStop()
  30.  * protected void onDestroy()
  31.  *
  32.  * 这七个方法定义了Activity的完整生命周期
  33.  * 实现这些方法可以帮助我们监视其中的三个嵌套生命周期循环
  34.  *
  35.  * Activity的完整生命周期自第一次调用onCreate()开始
  36.  * 直至调用onDestroy()为止
  37.  * Activity在onCreate()中设置所有"全局"状态以完成初始化,而在onDestroy()中释放所有系统资源
  38.  * 例如,如果Activity有一个线程在后台运行从网络上下载数据,它会在onCreate()创建线程,而在 onDestroy()销毁线程
  39.  *
  40.  * Activity的可视生命周期自onStart()调用开始直到相应的onStop()调用结束
  41.  * 在此期间,用户可以在屏幕上看到Activity,尽管它也许并不是位于前台或者也不与用户进行交互
  42.  * 在这两个方法之间,我们可以保留用来向用户显示这个Activity所需的资源
  43.  * 例如,当用户不再看见我们显示的内容时,我们可以在onStart()中注册一个BroadcastReceiver来监控会影响UI的变化
  44.  * 而在onStop()中来注消
  45.  * onStart() 和 onStop() 方法可以随着应用程序是否为用户可见而被多次调用。
  46.  *
  47.  * Activity的前台生命周期自onResume()调用起,至相应的onPause()调用为止
  48.  * 在此期间,Activity位于前台最上面并与用户进行交互
  49.  * Activity会经常在暂停和恢复之间进行状态转换——例如当设备转入休眠状态或者有新的Activity启动时
  50.  * 将调用onPause() 方法
  51.  * 当Activity获得结果或者接收到新的Intent时会调用onResume()方法
  52.  *
  53.  */
  54.  
  55. import android.app.Activity;
  56. import android.app.AlertDialog;
  57. import android.content.DialogInterface;
  58. import android.os.Bundle;
  59. import android.util.Log;
  60. import android.view.View;
  61. import android.widget.Button;
  62.  
  63. public class LifeActivity extends Activity {
  64.     private static final String TAG = "LifeActivity";
  65.  
  66.     @Override
  67.     public void onCreate(Bundle savedInstanceState) {
  68.         super.onCreate(savedInstanceState);
  69.         setContentView(R.layout.main);
  70.  
  71.         Log.i(TAG, "onCreate");
  72.  
  73.         Button button = (Button) this.findViewById(R.id.button);
  74.         button.setOnClickListener(new View.OnClickListener() {
  75.  
  76.             @Override
  77.             public void onClick(View arg0) {
  78.                 AlertDialog.Builder builder = new AlertDialog.Builder(LifeActivity.this);
  79.                 builder.setMessage("Are you sure you want to exit?")
  80.                     .setCancelable(false).setPositiveButton("Yes", new DialogInterface.OnClickListener() {
  81.                             public void onClick(DialogInterface dialog, int id) {
  82.                                         dialog.cancel();
  83.                             }}).setNegativeButton("No", new DialogInterface.OnClickListener() {
  84.                                     public void onClick(DialogInterface dialog, int id) {
  85.                                         dialog.cancel();
  86.                                     }
  87.                                 });
  88.                 AlertDialog alert = builder.create();
  89.                 alert.show();
  90.             }
  91.         });
  92.     }
  93.  
  94.     @Override
  95.     protected void onStart() {
  96.         Log.i(TAG, "onStart");
  97.         super.onStart();
  98.     }
  99.  
  100.     @Override
  101.     protected void onResume() {
  102.         Log.i(TAG, "onResume");
  103.         super.onResume();
  104.     }
  105.  
  106.     @Override
  107.     protected void onRestart() {
  108.         Log.i(TAG, "onRestart");
  109.         super.onRestart();
  110.     }
  111.  
  112.     @Override
  113.     protected void onPause() {
  114.         Log.i(TAG, "");
  115.         super.onPause();
  116.     }
  117.  
  118.     @Override
  119.     protected void onStop() {
  120.         Log.i(TAG, "onStop");
  121.         super.onStop();
  122.     }
  123.  
  124.     @Override
  125.     protected void onDestroy() {
  126.         Log.i(TAG, "onDestroy");
  127.         super.onDestroy();
  128.     }
  129.  
  130. }



阅读(748) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~