Activity的生命周期
Activity是整个Android平台的基本组成,生命周期主要包含三个阶段:
运行态(Running State)
此时Activity程序显示在屏幕前台,并且具有焦点,可以和用户的操作动作进行交互,例如:
向用户提供信息、捕获用户单击时的事件并作处理;
暂停态(Paused State)
此时Activity程序失去了焦点,并被其他处于运行态的Activity取代在屏幕前台显示,如果切换后的Activity
程序不能铺满整个屏幕窗口或者是本身具备透明效果,则该暂停态的Activity程序对用户依然可见,但是不可以
与其进行交互;
停止态(Stopped State)
停止态的Activity不仅没有焦点,而且是完全不可见的,虽然其也保留状态和成员等信息,但是停止态的Activity
会在系统需要的时候被结束。
Activity程序的生命周期控制方法:
| 方法 | 类型 | 是否可关闭 | 描述 |
1 | onCreate | 普通 | 不可以 | 当Activity程序启动之后会首先调用此方法 |
2 | onRestart | 普通 | 不可以 | 当Activity程序停止后再次显示给用户时调用 |
3 | onStart | 普通 | 不可以 | 当用户的一次显示界面时调用此方法 |
4 | onResume | 普通 | 不可以 | 当获得焦点时调用此方法 |
5 | onPause | 普通 | 可以 | 当启动其他Activity程序时调用此方法,用于进行数据的提交、动画处理的操作 |
6 | onStop | 普通 | 可以 | 当一个Activity程序完全不可见时调用此方法,此方法不会销毁Activty程序 |
7 | onDestroy | 普通 | 可以 | 程序被销毁时调用,当调用finish()方法或系统资源不够时将调用此方法 |
Activity基本生命周期(一)
第二个程序完全遮盖了第一个程序的情况
1、程序运行,并无任何操作
2、点击启动第二个Activity,跳转后第二个程序完全遮盖住了第一个程序
3、通过后退键回到第一个程序
4、如果是通过按钮返回的话,结果又不一样了
在firs_main.xml中:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:layout_marginTop="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textColor="#ffffff"
android:text="这是第一个程序"/>
<Button
android:id="@+id/mybut1"
android:layout_marginTop="8dp"
android:layout_width="180dp"
android:layout_height="40dp"
android:background="#3399ff"
android:textColor="#ffffff"
android:text="按我启动第二个Activity程序"/>
</LinearLayout>
在second_main.xml中:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:layout_marginTop="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textColor="#ffffff"
android:text="这是第二个程序"/>
<Button
android:id="@+id/mybut2"
android:layout_marginTop="8dp"
android:layout_width="180dp"
android:layout_height="40dp"
android:background="#3399ff"
android:textColor="#ffffff"
android:text="按我返回第一个Activity程序"/>
</LinearLayout>
在FirstActivity.java中:
package com.li.lifecycleproject;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.support.v4.app.NavUtils;
public class FirstActivity extends Activity {
private Button mybut1 = null;
@Override
public void onCreate(Bundle savedInstanceState) {
System.out.println("======(A) FirstActivity ---> onCreate()");
super.onCreate(savedInstanceState);
super.setContentView(R.layout.firs_main);
this.mybut1 = (Button)super.findViewById(R.id.mybut1);
this.mybut1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent it = new Intent(FirstActivity.this,SecondActivity.class);
FirstActivity.this.startActivity(it); //执行跳转
}});
}
@Override
protected void onDestroy() {
System.out.println("======(A) FirstActivity ---> onDestroy()");
super.onDestroy();
}
@Override
protected void onPause() {
System.out.println("======(A) FirstActivity ---> onPause()");
super.onPause();
}
@Override
protected void onRestart() {
System.out.println("======(A) FirstActivity ---> onRestart()");
super.onRestart();
}
@Override
protected void onResume() {
System.out.println("======(A) FirstActivity ---> onResume()");
super.onResume();
}
@Override
protected void onStart() {
System.out.println("======(A) FirstActivity ---> onStart()");
super.onStart();
}
@Override
protected void onStop() {
System.out.println("======(A) FirstActivity ---> onStop()");
super.onStop();
}
}
在SecondActivity.java中:
package com.li.lifecycleproject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SecondActivity extends Activity {
private Button mybut2 = null;
@Override
public void onCreate(Bundle savedInstanceState) {
System.out.println("======(B) SecondActivity ---> onCreate()");
super.onCreate(savedInstanceState);
super.setContentView(R.layout.second_main);
this.mybut2 = (Button)super.findViewById(R.id.mybut2);
this.mybut2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent it = new Intent(SecondActivity.this,FirstActivity.class);
SecondActivity.this.startActivity(it);
SecondActivity.this.finish();
}});
}
@Override
protected void onDestroy() {
System.out.println("======(B) SecondActivity ---> onDestroy()");
super.onDestroy();
}
@Override
protected void onPause() {
System.out.println("======(B) SecondActivity ---> onPause()");
super.onPause();
}
@Override
protected void onRestart() {
System.out.println("======(B) SecondActivity ---> onRestart()");
super.onRestart();
}
@Override
protected void onResume() {
System.out.println("======(B) SecondActivity ---> onResume()");
super.onResume();
}
@Override
protected void onStart() {
System.out.println("======(B) SecondActivity ---> onStart()");
super.onStart();
}
@Override
protected void onStop() {
System.out.println("======(B) SecondActivity ---> onStop()");
super.onStop();
}
}
修改AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.li.lifecycleproject"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".FirstActivity"
android:label="@string/title_activity_first" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"></activity>
</application>
</manifest>
Activity基本生命周期(二)
当按返回按钮的时候相当与调用了finish()方法
第二个程序没有完全遮盖第一个程序的情况
1、程序运行,并无任何操作
2、点击启动第二个Activity,跳转后第二个程序没有遮盖住第一个程序
3、按后退键返回
如果是通过按钮返回的话,结果又不一样了
在firs_main.xml中:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:layout_marginTop="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textColor="#ffffff"
android:text="这是第一个程序"/>
<Button
android:id="@+id/mybut1"
android:layout_marginTop="8dp"
android:layout_width="180dp"
android:layout_height="40dp"
android:background="#3399ff"
android:textColor="#ffffff"
android:text="按我启动第二个Activity程序"/>
</LinearLayout>
在second_main.xml中:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:layout_marginTop="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textColor="#ffffff"
android:text="这是第二个程序"/>
<Button
android:id="@+id/mybut2"
android:layout_marginTop="8dp"
android:layout_width="180dp"
android:layout_height="40dp"
android:background="#3399ff"
android:textColor="#ffffff"
android:text="按我返回第一个Activity程序"/>
</LinearLayout>
在FirstActivity.java中:
package com.li.lifecycleproject;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.support.v4.app.NavUtils;
public class FirstActivity extends Activity {
private Button mybut1 = null;
@Override
public void onCreate(Bundle savedInstanceState) {
System.out.println("======(A) FirstActivity ---> onCreate()");
super.onCreate(savedInstanceState);
super.setContentView(R.layout.firs_main);
this.mybut1 = (Button)super.findViewById(R.id.mybut1);
this.mybut1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent it = new Intent(FirstActivity.this,SecondActivity.class);
FirstActivity.this.startActivity(it); //执行跳转
}});
}
@Override
protected void onDestroy() {
System.out.println("======(A) FirstActivity ---> onDestroy()");
super.onDestroy();
}
@Override
protected void onPause() {
System.out.println("======(A) FirstActivity ---> onPause()");
super.onPause();
}
@Override
protected void onRestart() {
System.out.println("======(A) FirstActivity ---> onRestart()");
super.onRestart();
}
@Override
protected void onResume() {
System.out.println("======(A) FirstActivity ---> onResume()");
super.onResume();
}
@Override
protected void onStart() {
System.out.println("======(A) FirstActivity ---> onStart()");
super.onStart();
}
@Override
protected void onStop() {
System.out.println("======(A) FirstActivity ---> onStop()");
super.onStop();
}
}
在SecondActivity.java中:
package com.li.lifecycleproject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SecondActivity extends Activity {
private Button mybut2 = null;
@Override
public void onCreate(Bundle savedInstanceState) {
System.out.println("======(B) SecondActivity ---> onCreate()");
super.onCreate(savedInstanceState);
super.setContentView(R.layout.second_main);
this.mybut2 = (Button)super.findViewById(R.id.mybut2);
this.mybut2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent it = new Intent(SecondActivity.this,FirstActivity.class);
SecondActivity.this.startActivity(it);
SecondActivity.this.finish();
}});
}
@Override
protected void onDestroy() {
System.out.println("======(B) SecondActivity ---> onDestroy()");
super.onDestroy();
}
@Override
protected void onPause() {
System.out.println("======(B) SecondActivity ---> onPause()");
super.onPause();
}
@Override
protected void onRestart() {
System.out.println("======(B) SecondActivity ---> onRestart()");
super.onRestart();
}
@Override
protected void onResume() {
System.out.println("======(B) SecondActivity ---> onResume()");
super.onResume();
}
@Override
protected void onStart() {
System.out.println("======(B) SecondActivity ---> onStart()");
super.onStart();
}
@Override
protected void onStop() {
System.out.println("======(B) SecondActivity ---> onStop()");
super.onStop();
}
}
修改AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.li.lifecycleproject"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".FirstActivity"
android:label="@string/title_activity_first" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.Dialog" /> //第二个窗口以对话框形式显示
</application>
</manifest>
Activity基本生命周期(三)
第二个Activity使用finish()
1、程序运行,并无任何操作
2、按按钮启动第二个程序
4、按后退键返回,由于第二个程序使用了finish()方法,所以此时直接返回到第一个程序
在firs_main.xml中:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:layout_marginTop="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textColor="#ffffff"
android:text="这是第一个程序"/>
<Button
android:id="@+id/mybut1"
android:layout_marginTop="8dp"
android:layout_width="180dp"
android:layout_height="40dp"
android:background="#3399ff"
android:textColor="#ffffff"
android:text="按我启动第二个Activity程序"/>
</LinearLayout>
在second_main.xml中:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:layout_marginTop="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textColor="#ffffff"
android:text="这是第二个程序"/>
<Button
android:id="@+id/mybut2"
android:layout_marginTop="8dp"
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#3399ff"
android:textColor="#ffffff"
android:text="按我跳转到第三个Activity程序"/>
</LinearLayout>
在third_main.xml中:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:layout_marginTop="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textColor="#ffffff"
android:text="这是第三个程序"/>
</LinearLayout>
在FirstActivity.java中:
package com.li.lifecycleproject;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.support.v4.app.NavUtils;
public class FirstActivity extends Activity {
private Button mybut1 = null;
@Override
public void onCreate(Bundle savedInstanceState) {
System.out.println("======(A) FirstActivity ---> onCreate()");
super.onCreate(savedInstanceState);
super.setContentView(R.layout.firs_main);
this.mybut1 = (Button)super.findViewById(R.id.mybut1);
this.mybut1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent it = new Intent(FirstActivity.this,SecondActivity.class);
FirstActivity.this.startActivity(it); //执行跳转
}});
}
@Override
protected void onDestroy() {
System.out.println("======(A) FirstActivity ---> onDestroy()");
super.onDestroy();
}
@Override
protected void onPause() {
System.out.println("======(A) FirstActivity ---> onPause()");
super.onPause();
}
@Override
protected void onRestart() {
System.out.println("======(A) FirstActivity ---> onRestart()");
super.onRestart();
}
@Override
protected void onResume() {
System.out.println("======(A) FirstActivity ---> onResume()");
super.onResume();
}
@Override
protected void onStart() {
System.out.println("======(A) FirstActivity ---> onStart()");
super.onStart();
}
@Override
protected void onStop() {
System.out.println("======(A) FirstActivity ---> onStop()");
super.onStop();
}
}
在SecondActivity.java中:
package com.li.lifecycleproject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SecondActivity extends Activity {
private Button mybut2 = null;
@Override
public void onCreate(Bundle savedInstanceState) {
System.out.println("======(B) SecondActivity ---> onCreate()");
super.onCreate(savedInstanceState);
super.setContentView(R.layout.second_main);
this.mybut2 = (Button)super.findViewById(R.id.mybut2);
this.mybut2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent it = new Intent(SecondActivity.this,ThirdActivity.class);
SecondActivity.this.startActivity(it);
SecondActivity.this.finish();
}});
}
@Override
protected void onDestroy() {
System.out.println("======(B) SecondActivity ---> onDestroy()");
super.onDestroy();
}
@Override
protected void onPause() {
System.out.println("======(B) SecondActivity ---> onPause()");
super.onPause();
}
@Override
protected void onRestart() {
System.out.println("======(B) SecondActivity ---> onRestart()");
super.onRestart();
}
@Override
protected void onResume() {
System.out.println("======(B) SecondActivity ---> onResume()");
super.onResume();
}
@Override
protected void onStart() {
System.out.println("======(B) SecondActivity ---> onStart()");
super.onStart();
}
@Override
protected void onStop() {
System.out.println("======(B) SecondActivity ---> onStop()");
super.onStop();
}
}
在ThirdActivity.java中:
package com.li.lifecycleproject;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
@SuppressLint({ "ParserError", "ParserError" })
public class ThirdActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
System.out.println("======(C) ThirdActivity ---> onCreate()");
super.onCreate(savedInstanceState);
super.setContentView(R.layout.third_main);
}
@Override
protected void onDestroy() {
System.out.println("======(C) ThirdActivity ---> onDestroy()");
super.onDestroy();
}
@Override
protected void onPause() {
System.out.println("======(C) ThirdActivity ---> onPause()");
super.onPause();
}
@Override
protected void onRestart() {
System.out.println("======(C) ThirdActivity ---> onRestart()");
super.onRestart();
}
@Override
protected void onResume() {
System.out.println("======(C) ThirdActivity ---> onResume()");
super.onResume();
}
@Override
protected void onStart() {
System.out.println("======(C) ThirdActivity ---> onStart()");
super.onStart();
}
@Override
protected void onStop() {
System.out.println("======(C) ThirdActivity ---> onStop()");
super.onStop();
}
}
修改AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.li.lifecycleproject"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".FirstActivity"
android:label="@string/title_activity_first" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="@string/app_name"/>
<activity
android:name=".ThirdActivity"
android:label="@string/app_name"/>
</application>
</manifest>