在Android中,Activity就是布满整个窗口或者悬浮于其他窗口上的交互界面。在一个应用程序中通常由多个Activity构成,都会在Manifest.xml中指定一个主的Activity。
当启动其他的Activity时这个当前的这个Activity将会停止,新的Activity将会压入栈中,同时获取用户焦点,这时就可在这个Activity上操作了。从这个地方可以看出,应用中的所有的Activity其实就是压栈和弹出栈的过程。
1. Activity的生命周期
Activity生命周期的7种方法
7个方法如下:
1) void onCreate(Bundle savedInstanceState)
2) void onStart()
3) void onRestart()
4) void onResume()
5) void onPause()
6) void onStop()
7) void onDestroy()
操作过程如下:
1) 创建到销毁: onCreate --> onStart --> onResume -->onPause --> onStop --> onDestory
2) 创建状态: onCreate --> onStart --> onResume
3) 失去焦点: onPause --> onStop
4) 重新获得焦点: onRestart --> onStart --> onResume
5) 关闭状态: onPause --> onStop --> onDestory
程序Demo:
package com.android.lifetime; import android.os.Bundle; import android.app.Activity; import android.util.Log; public class LifeTime extends Activity { private final String TAG = "LifeTime"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Log.i(TAG, "onCreate method is executed!"); } @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); Log.i(TAG, "onStart method is executed!"); } @Override protected void onRestart() { // TODO Auto-generated method stub super.onRestart(); Log.i(TAG, "onRestart method is executed!"); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); Log.i(TAG, "onResume method is executed!"); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); Log.i(TAG, "onPause method is executed!"); } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); Log.i(TAG, "onStop method is executed!"); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); Log.i(TAG, "onDestroy method is executed!"); } }
2. AndroidManifest.xml 详解
<?xml version="1.0" encoding="utf-8"?> <!-- package 包表示整个Android应用程序的主要包名,而且是一个默认的名称 android:versionCode="1" 表示该工程所生成的apk版本号,1开始2、3、4不断的升级,以此来提示用户去不断升级 android:versionName="1.0" 表示版本的一个名称,通常是以 1.0开始的。 安装在手机中,如果应用程序比较大的话,建议选择auto,如果是比较大型的应用程序,比如游戏等,建议使用preferExternal属性,安装在外部存储器中 android:installLocation="auto" 自动寻找应用程序安装位置,ROM或者SDcard卡,默认的属性 android:installLocation="internalOnly" 仅仅只能安装在ROM中 android:installLocation="preferExternal" 仅仅只能安装在SDcard中 --> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.hello" android:versionCode="1" android:versionName="1.0" android:installLocation="auto" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <!-- android:name="com.android.hello.Hello" 或者是 android:name=".Hello" 表示与 package包名[package="com.android.hello"]底下的应用程序 intent-filter 意图过滤器:用来过滤用户的一些动作和操作 android.intent.action.MAIN : 表示当前的程序是整个工程的入口程序 android.intent.category.LAUNCHER : 表示决定应用程序是否在程序列表中显示 --> <activity android:name="com.android.hello.Hello" 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>