Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4027090
  • 博文数量: 366
  • 博客积分: 9916
  • 博客等级: 中将
  • 技术积分: 7195
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-29 23:27
个人简介

简单!

文章分类

全部博文(366)

文章存档

2013年(51)

2012年(269)

2011年(46)

分类: 嵌入式

2012-10-11 16:12:17

        Activity是Android中一个重要的组件。就像任务管理进程,每个Activity负责一个任务,一般都是一个页面对应一个 Activity,Activity负责响应当前页面的所有请求和处理。而Activity的生命周期,也和进程很类似,它有创建(OnCreate)、 开始(onStart)、暂停(onPause)、唤醒(onResume)、停止(onStop)、重启(onRestart)、销毁 (onDestroy)等状态。下面一幅图,清楚地描述了这些状态的转变:

        

           Activity启动的流程包括:onCreate()->onStart()->onResume()

           Activity关闭的流程包括:onPause()->onStop()->onDestroy()。

           Activity重启的流程包括:onPause()->onResume()或者onStop()->onRestart()

           下面再通过实例来讲述Activity的生命周期:(1)启动Activity1;(2)从Activity1中启动Activity2(注意Activity1这里并没有调用finish()方法);(3)从Activity2中返回Activity1(注意Activity2这里调用了finish()方法,会引发onDestroy()方法);(4)退出Activity1,程序结束。

         可以看出Activity1经历的生命周期为:onCreate()->onStart()->onResume()->onPause()->onStop()->onRestart()->onResume()->onPause()->onStop()->onDestroy()

           Activity2经历的生命周期为:onCreate()->onStart()->onResume()->onPause()->onStop()->onDestroy()


下面是代码的实现:

1、布局文件

           Activity1的布局文件main.xml:


  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android=""
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     >
  7.  <LinearLayout
  8.     android:orientation="horizontal"
  9.     android:layout_width="fill_parent"
  10.     android:layout_height="wrap_content"
  11.     >
  12.     <TextView
  13.         android:id="@+id/textView01"
  14.         android:layout_width="wrap_content"
  15.         android:layout_height="wrap_content"
  16.         android:text="输入字符串:"/>
  17.     <EditText
  18.         android:id="@+id/et_string"
  19.         android:layout_width="203px"
  20.         android:layout_height="47px"
  21.         android:textSize="18sp"/>
  22. </LinearLayout>
  23. <Button
  24.     android:id="@+id/bt_toActivity2"
  25.     android:layout_width="fill_parent"
  26.     android:layout_height="wrap_content"
  27.     android:text="计算字符串长度"/>
  28.  <LinearLayout
  29.     android:orientation="horizontal"
  30.     android:layout_width="fill_parent"
  31.     android:layout_height="wrap_content"
  32.     >
  33.    <TextView
  34.         android:id="@+id/textView02"
  35.         android:layout_width="wrap_content"
  36.         android:layout_height="wrap_content"
  37.         android:text="字符串长度为: "/>
  38.     <TextView
  39.     android:id="@+id/tv_result"
  40.     android:layout_width="202px"
  41.     android:layout_height="wrap_content"
  42.     android:textSize="18sp"/>
  43. </LinearLayout>
  44. <Button
  45.     android:id="@+id/bt_url"
  46.     android:layout_width="fill_parent"
  47.     android:layout_height="wrap_content"
  48.     android:text="打开百度网站"/>


  49. </LinearLayout>
Activity2的布局文件 mylayout.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android=""
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     >
  7.   <!-- 显示结果 -->
  8.  <TextView
  9.     android:id="@+id/tv_showResult"
  10.     android:layout_width="fill_parent"
  11.     android:layout_height="wrap_content"
  12.     android:text=""
  13.     />
  14.     <!-- 返回另一个Activity -->
  15.  <Button
  16.      android:id="@+id/bt_back"
  17.      android:layout_width="100px"
  18.      android:layout_height="wrap_content"
  19.      android:text="返回"
  20.      />
  21. </LinearLayout>

2、代码

           Activity1的程序代码


  1. package com.myandroid.test;

  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.content.SharedPreferences;
  5. import android.net.Uri;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. import android.view.View;
  9. import android.view.View.OnClickListener;
  10. import android.widget.Button;
  11. import android.widget.EditText;
  12. import android.widget.TextView;

  13. public class ActivityIntent extends Activity {
  14.     private Button bt_toActivity;
  15.     private Button bt_outsideIntent;
  16.     private EditText et_string;
  17.     private TextView tv_result;
  18.     
  19.     /** Called when the activity is first created. */
  20.     @Override
  21.     public void onCreate(Bundle savedInstanceState) {
  22.         super.onCreate(savedInstanceState);
  23.         setContentView(R.layout.main);
  24.         Log.e("Lifecycle_Activity1", "onCreate()");
  25.         
  26.         et_string = (EditText) findViewById(R.id.et_string);
  27.         bt_toActivity = (Button) findViewById(R.id.bt_toActivity2);
  28.         bt_outsideIntent = (Button) findViewById(R.id.bt_url);
  29.         tv_result = (TextView) findViewById(R.id.tv_result);
  30.         bt_toActivity.setOnClickListener(new ClickEvent());
  31.         bt_outsideIntent.setOnClickListener(new ClickEvent());
  32.     }

  33.     //销毁:onPause()->onStop()->onDestroy()
  34.     @Override    
  35.     protected void onDestroy() {    
  36.         super.onDestroy();    
  37.         Log.e("Lifecycle_Activity1", "onDestroy()");
  38.     }

  39.     //暂停:onStart()->onPause()
  40.     @Override    
  41.     protected void onPause() {        
  42.         super.onPause();
  43.         Log.e("Lifecycle_Activity1", "onPause()");
  44.         
  45.         //把数据保存到类似于Session之类的存储集合里面
  46.         SharedPreferences.Editor saveData = getPreferences(0).edit();
  47.         saveData.putString("value", et_string.getText().toString());
  48.         saveData.commit();
  49.     }
  50.     
  51.     //重启:onPause()->onResume()
  52.     @Override    
  53.     protected void onResume() {        
  54.         super.onResume();
  55.         Log.e("Lifecycle_Activity1", "onResume()");
  56.         
  57.         //从共享数据存储对象中获取所需的数据
  58.         SharedPreferences getData = getPreferences(0);
  59.         String value = getData.getString("value", null);
  60.         if(value != null) {
  61.             et_string.setText(value);
  62.         }
  63.     }
  64.     
  65.     //开始:onCreate()->onStart()
  66.     @Override
  67.     protected void onStart() {        
  68.         super.onStart();
  69.         Log.e("Lifecycle_Activity1", "onStart()");
  70.     }

  71.     //停止:onPause()->onStop()
  72.     @Override
  73.     protected void onStop() {        
  74.         super.onStop();
  75.         Log.e("Lifecycle_Activity1", "onStop()");
  76.     }

  77.     //重启:onStop()->reonRestart()
  78.     @Override
  79.     protected void onRestart() {        
  80.         super.onRestart();
  81.         Log.e("Lifecycle_Activity1", "onRestart()");
  82.     }
  83.     
  84.     
  85.     /**
  86.      * 处理上一个Activity的返回结果
  87.      */
  88.     @Override
  89.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  90.         // TODO Auto-generated method stub
  91.         super.onActivityResult(requestCode, resultCode, data);
  92.         switch(resultCode) {
  93.         
  94.         case RESULT_OK:    //执行成功,返回
  95.             Bundle bundle = data.getExtras();         //获取intent里面的bundle对象
  96.             int length = bundle.getInt("result");    
  97.             tv_result.setText("" + length);
  98.             break;
  99.             
  100.         default:
  101.         break;
  102.         }
  103.     }

  104.     //重写点击监听器类
  105.     class ClickEvent implements OnClickListener {

  106.         @Override
  107.         public void onClick(View v) {
  108.             // TODO Auto-generated method stub
  109.             
  110.             switch(v.getId()) {
  111.             
  112.             case R.id.bt_toActivity2:    //跳转到另一个Activity
  113.                 Intent intent = new Intent();
  114.                 intent = intent.setClass(ActivityIntent.this, AnotherActivity.class);
  115.                 Bundle bundle = new Bundle();
  116.                 bundle.putString("string", et_string.getText().toString());
  117.                 intent.putExtras(bundle);
  118.                 startActivityForResult(intent,0);    //采用Activity间信息交流启动方式,
  119.                                                     //比如A启动B,B结束后,返回到A的同时会执行    onActivityResult()方法
  120.                                                     //处理上一个Activity即B传递的结果集。    
  121.                 //ActivityIntent.this.finish();    //会触发onDestroy();
  122.                 break;
  123.                 
  124.             case R.id.bt_url:    //Intent还可以调用外部组件,但有些组件会有权限控制
  125.                 Uri uri = Uri.parse("");
  126.                 Intent outerIntent = new Intent(Intent.ACTION_VIEW, uri);
  127.                 startActivity(outerIntent);
  128.                 break;
  129.             default:
  130.                 break;
  131.             }
  132.         }
  133.         
  134.     }
  135. }
Activity2的程序代码

  1. package com.myandroid.test;

  2. import java.text.DecimalFormat;
  3. import java.text.NumberFormat;


  4. import android.app.Activity;
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. import android.view.View;
  9. import android.view.View.OnClickListener;
  10. import android.widget.Button;
  11. import android.widget.TextView;

  12. public class AnotherActivity extends Activity {
  13.     private String string;

  14.     /** Called when the activity is first created. */
  15.     @Override
  16.     public void onCreate(Bundle savedInstanceState) {
  17.         super.onCreate(savedInstanceState);
  18.         setContentView(R.layout.mylayout);
  19.         Log.e("Lifecycle_Activity1", "onCreate()");
  20.         
  21.         Intent intent = this.getIntent();         //获取intent对象
  22.         Bundle bundle = intent.getExtras();     //获取intent里面的bundle对象
  23.         string = bundle.getString("string");     //获取字符串
  24.         ((TextView)findViewById(R.id.tv_showResult)).setText(
  25.                 "字符串 " + string +" 长度为: " + string.length());
  26.         Button bt_back = (Button) findViewById(R.id.bt_back);
  27.         bt_back.setOnClickListener(new ClickEvent());

  28.     }
  29.     
  30.     @Override
  31.     protected void onDestroy() {    //销毁:onPause()->onStop()->onDestroy()
  32.         // TODO Auto-generated method stub
  33.         super.onDestroy();    
  34.         Log.e("Lifecycle_Activity2", "onDestroy()");
  35.     }

  36.     @Override
  37.     protected void onPause() {        //暂停:onStart()->onPause()
  38.         // TODO Auto-generated method stub
  39.         super.onPause();
  40.         Log.e("Lifecycle_Activity2", "onPause()");
  41.     }

  42.     @Override
  43.     protected void onResume() {        //重启:onPause()->onResume()
  44.         // TODO Auto-generated method stub
  45.         super.onResume();
  46.         Log.e("Lifecycle_Activity2", "onResume()");
  47.     }

  48.     @Override
  49.     protected void onStart() {        //开始:onCreate()->onStart()
  50.         // TODO Auto-generated method stub
  51.         super.onStart();
  52.         Log.e("Lifecycle_Activity2", "onStart()");
  53.     }

  54.     @Override
  55.     protected void onStop() {        //停止:onPause()->onStop()
  56.         // TODO Auto-generated method stub
  57.         super.onStop();
  58.         Log.e("Lifecycle_Activity2", "onStop()");
  59.     }

  60.     @Override
  61.     protected void onRestart() {        //重启
  62.         // TODO Auto-generated method stub
  63.         super.onRestart();
  64.         Log.e("Lifecycle_Activity2", "onRestart()");
  65.     }
  66.     
  67.     
  68.     class ClickEvent implements OnClickListener {

  69.         @Override
  70.         public void onClick(View v) {
  71.             // TODO Auto-generated method stub
  72.             
  73.             switch(v.getId()) {
  74.             case R.id.bt_back:
  75.                 Intent intent = new Intent();
  76.                 intent = intent.setClass(AnotherActivity.this, ActivityIntent.class);
  77.                 Bundle bundle = new Bundle();
  78.                 bundle.putInt("result", string.length());
  79.                 intent.putExtras(bundle);
  80.                 
  81.                 //返回刚刚创建的intent给上一个Activity,并且关闭当前Activity
  82.                 AnotherActivity.this.setResult(RESULT_OK, intent);
  83.                 AnotherActivity.this.finish();    //会触发onDestroy();
  84.                 break;
  85.             default:
  86.                 break;
  87.             }
  88.         }
  89.         
  90.     }
  91. }
最后记得在AndroidManifest.xml添加Activity2的信息,如下:

  1. <application android:icon="@drawable/icon" android:label="@string/app_name">
  2.    ............
  3.    <activity android:name="AnotherActivity"></activity>
  4. </application>



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