在Android的开发中,activity是非常重要的,它主要负责创建和显示窗口。也可以把 Activity理解成一个显示屏幕;在Android的应用中并不仅有一个Activity,而是可以有多个Activity存在。因其重要性,开发Android务必熟悉其生命周期。一个Activity的生命周期有7个函数。分别是:onCreate()、onStart()、 onResume()、 onPause() onStop()、 onDestroy()、 onRestart()。以上7个方法分别在4个阶段按照一定顺序调用。
(1)开始Activity:这个阶段一次执行3个生命周期方法:onCreat(),onStart(),onResume()。
(2)Activity失去焦点,就是用户不能对该Activity操作的时候,可以理解为失去了焦点。比如在Activity获得焦点的情况下进入第二个ACtivity,当前的Activity将失去焦点。在这一阶段依次调用onPause(),onStop()。
(3)Activity重新获得焦点:onRestart(),onStart(),onResume()。
(4)关闭Activity:onPause(),onStop(),onDesttroy()。
(5)在执行上述4个阶段的过程中,可以改变系统执行轨迹的生命周期方法是onPause(),onStop()。如果在执行onPause()方法过程中,Activity重新获得了焦点,则不执行onStop(),而是这样执行:onPause(),onResume()。
下面的例子说明在2个Activity中跳转时,各个Activity执行的状态。
第一个Activity
主界面:main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".Android_cycleActivity" >
<TextView
android:id="@+id/tv01"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:text="com.dong.Android_cycle." />
<Button
android:id="@+id/btn1"
android:layout_width="fill_parent"
android:layout_height="79dp"
android:text="启动第二个Activity" />
</LinearLayout>
第二个程序Activity
主界面 main2.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/btn2"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="跳回" />
</LinearLayout>
strings.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Android_cycleActivity</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
</resources>
功能清单文件
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dong.android_cycleactivity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.dong.android_cycleactivity.Android_cycleActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.dong.android_cycleactivity.MainActivity"
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>
主程序
Android_cycleActivity.java
package com.dong.android_cycleactivity;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.content.Intent;
public class Android_cycleActivity extends Activity {
private Button btn;
private TextView tv01;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_cycle);
tv01=(TextView)findViewById(R.id.tv01);
tv01.setText(this+""); //打印activity的ID
System.out.println("first onCreate");
btn=(Button)findViewById(R.id.btn1);
btn.setOnClickListener(new ClickEvent());
}
class ClickEvent implements OnClickListener{
public void onClick(View v)
{
Intent intent=new Intent();
intent.setClass(Android_cycleActivity.this, MainActivity.class);
Android_cycleActivity.this.startActivity(intent);
//finish();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.android_cycle, menu);
return true;
}
@Override
protected void onStart()
{ //TODO Auto-generated method stub
System.out.println("first onStart");
super.onStart();
}
@Override
protected void onRestart()
{ //TODO Auto-generated method stub
System.out.println("first onRestart");
super.onRestart();
}
@Override
protected void onResume()
{ //TODO Auto-generated method stub
System.out.println("first onResume");
super.onResume();
}
@Override
protected void onPause()
{ //TODO Auto-generated method stub
System.out.println("first onPause");
super.onPause();
}
@Override
protected void onStop()
{ //TODO Auto-generated method stub
System.out.println("first onStop");
super.onStop();
}
@Override
protected void onDestroy()
{ //TODO Auto-generated method stub
System.out.println("first onDestroy");
super.onDestroy();
}
}
第二个程序
MainActivity.java
package com.dong.android_cycleactivity;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button btn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("Second OnCreate");
btn2=(Button)findViewById(R.id.btn2);
btn2.setOnClickListener(new ClickEvent());
}
class ClickEvent implements OnClickListener{
public void onClick(View v)
{
Intent intent=new Intent();
intent.setClass(MainActivity.this, Android_cycleActivity.class);
MainActivity.this.startActivity(intent);
//finish();
}
}
/*
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
*/
@Override
protected void onStart()
{ //TODO Auto-generated method stub
System.out.println("second onStart");
super.onStart();
}
@Override
protected void onRestart()
{ //TODO Auto-generated method stub
System.out.println("second onRestart");
super.onRestart();
}
@Override
protected void onResume()
{ //TODO Auto-generated method stub
System.out.println("second onResume");
super.onResume();
}
@Override
protected void onPause()
{ //TODO Auto-generated method stub
System.out.println("second onPause");
super.onPause();
}
@Override
protected void onStop()
{ //TODO Auto-generated method stub
System.out.println("second onStop");
super.onStop();
}
@Override
protected void onDestroy()
{ //TODO Auto-generated method stub
System.out.println("second onDestroy");
super.onDestroy();
}
}
(1)开始Activity:这个阶段一次执行3个生命周期方法:onCreat(),onStart(),onResume()。
(2)Activity失去焦点,就是用户不能对该Activity操作的时候,可以理解为失去了焦点。比如在Activity获得焦点的情况下进入第二个ACtivity,当前的Activity将失去焦点。在这一阶段依次调用onPause(),onStop()。
(3)Activity重新获得焦点:onRestart(),onStart(),onResume()。
(4)关闭Activity:onPause(),onStop(),onDesttroy()。
(5)在执行上述4个阶段的过程中,可以改变系统执行轨迹的生命周期方法是onPause(),onStop()。如果在执行onPause()方法过程中,Activity重新获得了焦点,则不执行onStop(),而是这样执行:onPause(),onResume()。
下面的例子说明在2个Activity中跳转时,各个Activity执行的状态。
第一个Activity
主界面:main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".Android_cycleActivity" >
<TextView
android:id="@+id/tv01"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:text="com.dong.Android_cycle." />
<Button
android:id="@+id/btn1"
android:layout_width="fill_parent"
android:layout_height="79dp"
android:text="启动第二个Activity" />
</LinearLayout>
第二个程序Activity
主界面 main2.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/btn2"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="跳回" />
</LinearLayout>
strings.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Android_cycleActivity</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
</resources>
功能清单文件
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dong.android_cycleactivity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.dong.android_cycleactivity.Android_cycleActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.dong.android_cycleactivity.MainActivity"
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>
主程序
Android_cycleActivity.java
package com.dong.android_cycleactivity;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.content.Intent;
public class Android_cycleActivity extends Activity {
private Button btn;
private TextView tv01;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_cycle);
tv01=(TextView)findViewById(R.id.tv01);
tv01.setText(this+""); //打印activity的ID
System.out.println("first onCreate");
btn=(Button)findViewById(R.id.btn1);
btn.setOnClickListener(new ClickEvent());
}
class ClickEvent implements OnClickListener{
public void onClick(View v)
{
Intent intent=new Intent();
intent.setClass(Android_cycleActivity.this, MainActivity.class);
Android_cycleActivity.this.startActivity(intent);
//finish();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.android_cycle, menu);
return true;
}
@Override
protected void onStart()
{ //TODO Auto-generated method stub
System.out.println("first onStart");
super.onStart();
}
@Override
protected void onRestart()
{ //TODO Auto-generated method stub
System.out.println("first onRestart");
super.onRestart();
}
@Override
protected void onResume()
{ //TODO Auto-generated method stub
System.out.println("first onResume");
super.onResume();
}
@Override
protected void onPause()
{ //TODO Auto-generated method stub
System.out.println("first onPause");
super.onPause();
}
@Override
protected void onStop()
{ //TODO Auto-generated method stub
System.out.println("first onStop");
super.onStop();
}
@Override
protected void onDestroy()
{ //TODO Auto-generated method stub
System.out.println("first onDestroy");
super.onDestroy();
}
}
第二个程序
MainActivity.java
package com.dong.android_cycleactivity;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button btn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("Second OnCreate");
btn2=(Button)findViewById(R.id.btn2);
btn2.setOnClickListener(new ClickEvent());
}
class ClickEvent implements OnClickListener{
public void onClick(View v)
{
Intent intent=new Intent();
intent.setClass(MainActivity.this, Android_cycleActivity.class);
MainActivity.this.startActivity(intent);
//finish();
}
}
/*
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
*/
@Override
protected void onStart()
{ //TODO Auto-generated method stub
System.out.println("second onStart");
super.onStart();
}
@Override
protected void onRestart()
{ //TODO Auto-generated method stub
System.out.println("second onRestart");
super.onRestart();
}
@Override
protected void onResume()
{ //TODO Auto-generated method stub
System.out.println("second onResume");
super.onResume();
}
@Override
protected void onPause()
{ //TODO Auto-generated method stub
System.out.println("second onPause");
super.onPause();
}
@Override
protected void onStop()
{ //TODO Auto-generated method stub
System.out.println("second onStop");
super.onStop();
}
@Override
protected void onDestroy()
{ //TODO Auto-generated method stub
System.out.println("second onDestroy");
super.onDestroy();
}
}