概述
研究activity跳转时的生命周期,分完全覆盖的activity跳转,与不完全覆盖的
生命周期
代码
MainActivity
package test.activitylife;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button onPause;
private Button onStop;
private static final String TAG = MainActivity.class.getSimpleName();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViews();
onPause.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, PauseActivity.class);
startActivity(intent);
}
});
onStop.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
startActivity(intent);
}
});
Log.v(TAG, "---------------onCreate--------------------");
}
@Override
protected void onStart() {
Log.v(TAG, "---------------onStart--------------------");
super.onStart();
}
@Override
protected void onRestart() {
Log.v(TAG, "---------------onRestart--------------------");
super.onRestart();
}
@Override
protected void onResume() {
Log.v(TAG, "---------------onResume--------------------");
super.onResume();
}
@Override
protected void onPause() {
Log.v(TAG, "---------------onPause--------------------");
super.onPause();
}
@Override
protected void onStop() {
Log.v(TAG, "---------------onStop--------------------");
super.onStop();
}
@Override
protected void onDestroy() {
Log.v(TAG, "---------------onDestroy--------------------");
super.onDestroy();
}
public void findViews() {
onPause = (Button)findViewById(R.id.dialog);
onStop = (Button)findViewById(R.id.activity);
}
}
OtherActivity
package test.activitylife;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class OtherActivity extends Activity {
private static final String TAG=OtherActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.v(TAG, "---------------onCreate--------------------");
super.onCreate(savedInstanceState);
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
Log.v(TAG, "---------------onStart--------------------");
super.onStart();
}
@Override
protected void onRestart() {
Log.v(TAG, "---------------onRestart--------------------");
super.onRestart();
}
@Override
protected void onResume() {
Log.v(TAG, "---------------onResume--------------------");
super.onResume();
}
@Override
protected void onPause() {
Log.v(TAG, "---------------onPause--------------------");
super.onPause();
}
@Override
protected void onStop() {
Log.v(TAG, "---------------onStop--------------------");
super.onStop();
}
@Override
protected void onDestroy() {
Log.v(TAG, "---------------onDestroy--------------------");
super.onDestroy();
}
}
PauseActivity
package test.activitylife;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class PauseActivity extends Activity {
private static final String TAG=PauseActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
Log.v(TAG, "---------------onCreate--------------------");
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
}
@Override
protected void onStart() {
Log.v(TAG, "---------------onStart--------------------");
super.onStart();
}
@Override
protected void onRestart() {
Log.v(TAG, "---------------onRestart--------------------");
super.onRestart();
}
@Override
protected void onResume() {
Log.v(TAG, "---------------onResume--------------------");
super.onResume();
}
@Override
protected void onPause() {
Log.v(TAG, "---------------onPause--------------------");
super.onPause();
}
@Override
protected void onStop() {
Log.v(TAG, "---------------onStop--------------------");
super.onStop();
}
@Override
protected void onDestroy() {
Log.v(TAG, "---------------onDestroy--------------------");
super.onDestroy();
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="hello" />
<Button
android:id="@+id/dialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="OnPause"
/>
<Button
android:id="@+id/activity"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Onstop"
/>
</LinearLayout>
other.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.activitylife"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="9" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<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=".OtherActivity"></activity>
<!-- dialog风格的Activity,没有完全覆盖当前Activity -->
<activity android:name=".PauseActivity" android:theme="@android:style/Theme.Dialog"></activity>
</application>
</manifest>
测试结果
OtherActivity完全覆盖MainActivity
点击 OnStop
可以发现在第二个activity的OnResume之后,第一个activity才OnStop,但是在第二个activity的Oncreate之前就执行了OnPause。也就是说只有在第二个activity完全起来之后才调用第一个activity的OnStop
按 返回
PauseActivity不完全覆盖MainActivity
点击 OnPause
这里只调了第一个activity的OnPause,而没有调用OnStop
按 返回
参考资料
http://blog.csdn.net/android_tutor/article/details/5772285