Android—Activity生命周期
一、图解
二、概念分析
当一个活动被创建时,调用onCreate() 方法
当一个活动能被用户看到(即可见)时,调用onStart() 方法
当一个活动可获取用户焦点时,调用onResume() 方法
所以打开一个活动时,会先后执行onCreate()->onStart()->onResume三个方法
当一个活动不能获取用户焦点时(此时另一个活动进入前台),调用onPause() 方法
当一个活动不可见(即被其他活动完全覆盖)时,调用onStop() 方法
当我们准备启动另一个新的活动覆盖当前活动时时,会依次调用当前活动类的
onPause() -> onStop() 方法
当我们再次启动该活动,该活动由后台转到前台运行时,会依次调用该活动类的onRestart()->onStart()->onResume()三个方法
当我们退出程序,或调用finish() 方法时,则依次调用 onPause() -> onStop() -> onDestroy() 方法
三、例子说明
//FirstActivity.java
package mars.activity05;
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 FirstActivity extends Activity {
/** Called when the activity is first created. */
private Button myButton;
@Override
public void onCreate(Bundle savedInstanceState) {
System.out.println("FirstActivity ---> onCreate ");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myButton = (Button) findViewById(R.id.myButton);
myButton.setOnClickListener(new ButtonListener());
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
System.out.println("FirstAcvity --->onDestory");
super.onDestroy();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
System.out.println("FirstAcvity --->onPause");
super.onPause();
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
System.out.println("FirstAcvity --->onRestart");
super.onRestart();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
System.out.println("FirstAcvity --->onResume");
super.onResume();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
System.out.println("FirstAcvity --->onStart");
super.onStart();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
System.out.println("FirstAcvity --->onStop");
super.onStop();
}
class ButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(FirstActivity.this, SecondActivity.class);
intent.putExtra("extra_data", "Hello I'm FirstActivity!");
FirstActivity.this.startActivity(intent);
}
}
}
//mian.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/myButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/first_button"
/>
</LinearLayout>
//SecondActivity.java
package mars.activity05;
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;
import android.widget.TextView;
public class SecondActivity extends Activity{
private Button secondButton;
TextView text = null;
String str = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
System.out.println("SecondActivity--->onCreate");
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
secondButton = (Button)findViewById(R.id.secondButton);
secondButton.setOnClickListener(new ButtonListener());
text = (TextView) findViewById (R.id.text);
Intent intent = getIntent();
str = intent.getStringExtra("extra_data");
text.setText(str);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
System.out.println("SecondActivity--->onDestory");
super.onDestroy();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
System.out.println("SecondActivity--->onPause");
super.onPause();
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
System.out.println("SecondActivity--->onRestart");
super.onRestart();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
System.out.println("SecondActivity--->onResume");
super.onResume();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
System.out.println("SecondActivity--->onStart");
super.onStart();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
System.out.println("SecondActivity--->onStop");
super.onStop();
}
class ButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(SecondActivity.this, FirstActivity.class);
SecondActivity.this.startActivity(intent);
}
}
}
//second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/second_button"
></TextView>
<Button
android:id="@+id/secondButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/second_button"
/>
</LinearLayout>
//AndroidManifest.xml 中注册 SecondActivity
<activity android:name=".SecondActivity"
android:label="SecondActivity"
android:theme="@android:style/Theme.Dialog"/>
//string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, FirstActivity!</string>
<string name="app_name">activity05</string>
<string name="first_button">启动第二个Activity</string>
<string name="second_button">回到第一个Activity</string>
</resources>