1. 训练目标
1) 观察Activity生命周期经历的主要方法
2) 如果使用LogCat观察输出,掌握如何设置LogFilter
3) 观察何时调用onPause方法?
4) 观察何时调用onStop方法?
2.核心代码。
<pre name="code" class="java">[java] view plain copy
package com.test4.lifecycle;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("MainActivity","onCreat");
}
public void toNormal(View view){
Intent intentNormal = new Intent (this, NormalActivity.class);
startActivity(intentNormal);
}
public void toDialog(View view){
Intent intentDialog = new Intent (this, DialogActivity.class);
startActivity(intentDialog);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.i("MainActivity","onDestroy");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.i("MainActivity","onPause");
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Log.i("MainActivity","onRestart");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.i("MainActivity","onResume");
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.i("MainActivity","onStart");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.i("MainActivity","onStop");
}
@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
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
NormalActivity和DialogActivity并没有进行改动。
activity_main:
[html] view plain copy
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.test4.lifecycle.MainActivity" >
<Button
android:id="@+id/toNormal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="136dp"
android:onClick="toNormal"
android:text="@string/toNormal" />
<Button
android:id="@+id/toDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/toNormal"
android:layout_centerHorizontal="true"
android:layout_marginTop="27dp"
android:onClick="toDialog"
android:text="@string/toDialog" />
</RelativeLayout>
activity_normal:
[html] view plain copy
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.test4.lifecycle.NormalActivity" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Normal_output"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
activity_dialog:
[html] view plain copy
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.test4.lifecycle.DialogActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Dialog_output"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
AndroidManifest.xml(清单文件中的一部分):
[html] view plain copy
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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>
<activity
android:name=".NormalActivity"
android:label="@string/title_activity_normal" >
</activity>
<activity
android:name=".DialogActivity"
android:theme="@android:style/Theme.Dialog"
android:label="@string/title_activity_dialog" >
</activity>
</application>
3.运行效果图。