Android 学习记录3 -Activity 的生命周期
2012-05-01 22:53
671 查看
学习记录3
Activity的生命周期
有四个状态:
1. 活动状态:当前Activity位于前台,用户可见,可以获得焦点
2. 暂停状态:其他Activity位于前台,该Activity依然可见,只是不能获得焦点。
3. 停止状态:该Activity不可见,失去焦点。
4. 销毁状态:该Activity结束,或Activity所在的Dalvik进程被结束。
图3-1 Activity生命周期以及回调方法
从图3-1所示,在Activity的生命周期中,有如下方法:
onCreate():创建Activity时被回调。
onStart():启动Activity时被回调。
onRestart():重新启动Activity时被回调。
onResume():恢复Activity时被回调。
onPause():暂停Activity时被回调。
onStop():停止Activity时被回调。
onDestroy:销毁Activity时被回调。
Lifecycle项目的主要代码:
LifecycleActivity.java源码:
main.xml的源码:
strings.xml的源码:
AndroidManifest.xml的源码:
在虚拟机运行效果图:
通过DDMS的LogCat窗口可以看到启动初启动Activity的输出:
暂停Activity时返回时回调的方法:
重新启动应用程序:
按“退出“按钮时结束Activity时回调的方法:
上述为整个Activity的生命周期的各种状态及不同状态之间切换时所回调的方法。
Activity的生命周期
有四个状态:
1. 活动状态:当前Activity位于前台,用户可见,可以获得焦点
2. 暂停状态:其他Activity位于前台,该Activity依然可见,只是不能获得焦点。
3. 停止状态:该Activity不可见,失去焦点。
4. 销毁状态:该Activity结束,或Activity所在的Dalvik进程被结束。
图3-1 Activity生命周期以及回调方法
从图3-1所示,在Activity的生命周期中,有如下方法:
onCreate():创建Activity时被回调。
onStart():启动Activity时被回调。
onRestart():重新启动Activity时被回调。
onResume():恢复Activity时被回调。
onPause():暂停Activity时被回调。
onStop():停止Activity时被回调。
onDestroy:销毁Activity时被回调。
Lifecycle项目的主要代码:
LifecycleActivity.java源码:
package com.Lifecycle; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; /** * Description: * <br/>site: <a href="http://www.crazyit.org">crazyit.org</a> * <br/>Copyright (C), 2001-2012, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * @author Yeeku.H.Lee kongyeeku@163.com * @version 1.0 */ public class LifecycleActivity extends Activity { final String TAG = "--CrazyIt--"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //输出日志 Log.d(TAG , "-------onCreate------"); Button bn = (Button)findViewById(R.id.bn); //为bn按钮绑定事件监听器 bn.setOnClickListener(new OnClickListener() { public void onClick(View source) { //结束该Activity LifecycleActivity.this.finish(); } }); } @Override public void onStart() { super.onStart(); //输出日志 Log.d(TAG , "-------onStart------"); } @Override public void onRestart() { super.onRestart(); //输出日志 Log.d(TAG , "-------onRestart------"); } @Override public void onResume() { super.onResume(); //输出日志 Log.d(TAG , "-------onResume------"); } @Override public void onPause() { super.onPause(); //输出日志 Log.d(TAG , "-------onPause------"); } @Override public void onStop() { super.onStop(); //输出日志 Log.d(TAG , "-------onStop------"); } @Override public void onDestroy() { super.onDestroy(); //输出日志 Log.d(TAG , "-------onDestroy------"); } }
main.xml的源码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/bn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="退出" /> </LinearLayout>
strings.xml的源码:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, Lifecycle!</string> <string name="app_name">生命周期演示</string> </resources>
AndroidManifest.xml的源码:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.Lifecycle" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".LifecycleActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
在虚拟机运行效果图:
通过DDMS的LogCat窗口可以看到启动初启动Activity的输出:
暂停Activity时返回时回调的方法:
重新启动应用程序:
按“退出“按钮时结束Activity时回调的方法:
上述为整个Activity的生命周期的各种状态及不同状态之间切换时所回调的方法。
相关文章推荐
- Android 学习记录3 -Activity 的生命周期
- Android 学习记录3 -Activity 的生命周期
- 记录自己的Android学习(三)Activity生命周期
- Android 学习记录-Activity生命周期
- Android学习——Activity的生命周期
- Android学习之横竖屏切换与Activity生命周期的关系
- Android06之学习日常(Activity生命周期*)
- android学习历程5--activity生命周期
- Android 学习笔记--android——Activity生命周期,server,BroadCastReciver
- Activity 生命周期(一)-- MarsChen Android 开发教程学习笔记
- Android学习——Activity的生命周期
- Android学习整理之Activity生命周期篇
- Android学习之Activity生命周期(一)
- Android学习总结(一)——Activity的基本概念与Activity的生命周期
- Android学习之Activity生命周期文档翻译
- Android学习之横竖屏切换与Activity生命周期的关系
- android学习之Activity的生命周期理解
- 1.4_Android Training 学习笔记_管理 Activity 的生命周期
- android学习四(Activity的生命周期)
- android开发学习笔记(二)-activity的生命周期