为了查看Activity切换经历的生命周期,首先看下面代码:
布局文件
main.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:text="@+id/Button01"
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
</LinearLayout>
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:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</TextView>
<Button
android:id="@+id/secondButton"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="second_button"
/>
</LinearLayout>
MainActivity代码
public class MainActivity extends Activity {
private Button myButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myButton=(Button)findViewById(R.id.Button01);
myButton.setOnClickListener(new ButtonListener());
Log.e("","MainActivity.onCreate()");
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.e("","MainActivity.onDestroy()");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.e("","MainActivity.onPause()");
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.e("","MainActivity.onStart()");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.e("","MainActivity.onStop()");
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Log.e("","MainActivity.onRestart()");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.e("","MainActivity.onResume()");
}
class ButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.setClass(MainActivity.this,SecondActivity.class);
MainActivity.this.startActivity(intent);
}
}
}
public class SecondActivity extends Activity{
private Button secondButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
secondButton = (Button)findViewById(R.id.secondButton);
secondButton.setOnClickListener(new ButtonListener());
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.e("","SecondActivity.onStart()");
}
@Override
protected void onResume() {
super.onResume();
Log.e("","SecondActivity.onResume()");
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Log.e("","SecondActivity.onReStart()");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.e("","SecondActivity.onStop()");
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.e("","SecondActivity.onDestroy()");
}
class ButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(SecondActivity.this, MainActivity.class);
SecondActivity.this.startActivity(intent);
}
}
}
首先运行结果为:
MainActivity.onCreate()
MainActivity.onStart()
MainActivity.onResume()
点击按钮之后执行为
MainActivity.onPause()
SecondActivity.onCreate()
SecondActivity.onStart()
SecondActivity.onResume()
MainActivity.onStop()
从SecondActivity切换到Main会执行
SecondActivity.onPause()
MainActivity.onCreate()
MainActivity.onStart()
MainActivity.onResume()
SecondActivity.onStop()
======更改一下代码 ,将SecondActivity的一部分代码增加一行SecondActivity.this.finish();
这时运行结果为:
SecondActivity.onStop()
SecondActivity.onDestroy()
进一步修改,将MainActivity中继承的Activity修改为ActivityGroup,执行结果为:
下面看下Activity与ActivityGroup一起使用时生命周期过程
布局文件
main.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"
>
<LinearLayout android:id="@+id/LinearLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content">
<Button android:text="@+id/Button01"
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button android:text="@+id/Button02"
android:id="@+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
<LinearLayout android:id="@+id/LinearLayout02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
</LinearLayout>
listview.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ListView android:id="@+id/MyListView" android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
</LinearLayout>
MainActivity
public class MainActivity extends ActivityGroup {
private Button btn1,btn2;
private LinearLayout container;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
container=(LinearLayout)findViewById(R.id.LinearLayout02);
btn1=(Button)findViewById(R.id.Button01);
btn2=(Button)findViewById(R.id.Button02);
btn1.setOnClickListener(new ClickEvent());
btn2.setOnClickListener(new ClickEvent());
Log.e("Main", "MainActivity====================>onCreate");
}
class ClickEvent implements OnClickListener
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
container.removeAllViews();
Intent intent=new Intent(MainActivity.this,SubActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
String[] str=new String[12];
if(v==btn1)
{
for(int i=0;i<str.length;i++)
str[i]="单选"+String.valueOf(i);
intent.putExtra("Name", "subActivity1");
intent.putExtra("Strings", str);
intent.putExtra("ChoiceMode", ListView.CHOICE_MODE_SINGLE);//通过参数设置列
}else if(v==btn2){
for(int i=0;i<str.length;i++)
str[i]="复选"+String.valueOf(i);
intent.putExtra("Name", "subActivity2");
intent.putExtra("Strings", str);
intent.putExtra("ChoiceMode", ListView.CHOICE_MODE_MULTIPLE);//通过参数设置列表式样
}
Window subActivity=getLocalActivityManager().startActivity("subActivity",intent);
container.addView(subActivity.getDecorView());
}
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.e("Main", "MainActivity====================>onStart");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.e("Main", "MainActivity====================>onResume");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.e("Main", "MainActivity====================>onPause");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.e("Main", "MainActivity====================>onStop");
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.e("Main", "MainActivity====================>onDestroy");
}
}
public class SubActivity extends Activity{
private String name;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
String[] str = this.getIntent().getStringArrayExtra("Strings");
int choiceMode = this.getIntent().getIntExtra("ChoiceMode",
ListView.CHOICE_MODE_NONE);
ListView listView = (ListView) findViewById(R.id.MyListView);
// 设置列表的式样
int itemID = android.R.layout.simple_list_item_1;
if (choiceMode == ListView.CHOICE_MODE_MULTIPLE)// 主Activity要求多选
itemID = android.R.layout.simple_list_item_multiple_choice;
else if (choiceMode == ListView.CHOICE_MODE_SINGLE)// 主Activity要求单选
itemID = android.R.layout.simple_list_item_single_choice;
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
itemID, str);
listView.setAdapter(arrayAdapter);
listView.setChoiceMode(choiceMode);
Log.e(name, "SubActivity====================>onCreate");// 显示当前状态,onCreate与onDestroy对应
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.e(name, "SubActivity====================>onStart");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.e(name, "SubActivity====================>onResume");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.e(name, "SubActivity====================>onPause");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.e(name, "SubActivity====================>onStop");
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.e(name, "SubActivity====================>onDestroy");
}
}
运行结果如下:
MainActivity====================>onCreate
MainActivity====================>onStart
MainActivity====================>onResume
点击TAB1,执行
SubActivity====================>onCreate
SubActivity====================>onStart
SubActivity====================>onResume
切换到TAB2,执行
SubActivity====================>onPause
SubActivity====================>onStop
SubActivity====================>onDestroy
SubActivity====================>onCreate
SubActivity====================>onStart
SubActivity====================>onResume
一个更为复杂的场景如下:
1.一个主界面MainActivity(继承ActivityGroup) 点击“呼叫”按钮
进入到呼叫界面 Call(继承Activity,实现SurfaceHolder.Callback接口)
2.如果用户长时间未接电话,自动挂断并返回到MainActivity界面
进入的生命周期函数依次为
onCreate(Main)-->onStart(Main)->onResume(Main)-->onPause(Main)-->onCreate(Call)-->onStart(Call)-->onResume(Call)-->surfaceCreated(Call)-->surfaceChanged(Call)-->onStop(Main)
没有人接听,由消息事件触发:
onPause(Call)-->onDestroy(Main)-->onCreate(Main)-->onStart(Main)->onResume(Main)---->surfaceDestroyed(Call)-->onStop(Call)-->onDestroyed(Call)