package com.horsttnann.life;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class MainActivity extends Activity {
private static final String TAG = MainActivity.class.getSimpleName();
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate...");
findViews();
}
private void findViews() {
mTextView = (TextView) findViewById(R.id.tv);
mTextView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.d(TAG, "onRestoreInstanceState...");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d(TAG, "onRestart...");
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart...");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume...");
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.d(TAG, "onSaveInstanceState...");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause...");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop...");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy...");
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.d(TAG, "onConfigurationChanged...");
}
}
第一种情况,不配置android:configChanges。
<activity
android:name="com.horsttnann.life.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>
例子1:
进入MainActivity
03-25 09:51:56.485: D/MainActivity(10495): onCreate...
03-25 09:51:56.485: D/MainActivity(10495): onStart...
03-25 09:51:56.485: D/MainActivity(10495): onResume...
按返回键
03-25 09:54:33.875: D/MainActivity(10495): onPause...
03-25 09:54:33.975: D/MainActivity(10495): onStop...
03-25 09:54:33.985: D/MainActivity(10495): onDestroy...
例子2:
进入MainActivity
03-25 09:55:13.525: D/MainActivity(10495): onCreate...
03-25 09:55:13.525: D/MainActivity(10495): onStart...
03-25 09:55:13.525: D/MainActivity(10495): onResume...
从竖屏切换到横屏
03-25 09:55:44.145: D/MainActivity(10495): onSaveInstanceState...
03-25 09:55:44.145: D/MainActivity(10495): onPause...
03-25 09:55:44.145: D/MainActivity(10495): onStop...
03-25 09:55:44.145: D/MainActivity(10495): onDestroy...
03-25 09:55:44.185: D/MainActivity(10495): onCreate...
03-25 09:55:44.185: D/MainActivity(10495): onStart...
03-25 09:55:44.185: D/MainActivity(10495): onRestoreInstanceState...
03-25 09:55:44.185: D/MainActivity(10495): onResume...
再从横屏切换到竖屏
03-25 09:56:02.855: D/MainActivity(10495): onSaveInstanceState...
03-25 09:56:02.855: D/MainActivity(10495): onPause...
03-25 09:56:02.855: D/MainActivity(10495): onStop...
03-25 09:56:02.855: D/MainActivity(10495): onDestroy...
03-25 09:56:02.915: D/MainActivity(10495): onCreate...
03-25 09:56:02.915: D/MainActivity(10495): onStart...
03-25 09:56:02.915: D/MainActivity(10495): onRestoreInstanceState...
03-25 09:56:02.915: D/MainActivity(10495): onResume...
例子3:
进入MainActivity
03-25 10:14:09.919: D/MainActivity(5354): onCreate...
03-25 10:14:09.919: D/MainActivity(5354): onStart...
03-25 10:14:09.919: D/MainActivity(5354): onResume...
切换到SecondActivity
03-25 10:14:13.509: D/MainActivity(5354): onSaveInstanceState...
03-25 10:14:13.509: D/MainActivity(5354): onPause...
03-25 10:14:13.629: D/MainActivity(5354): onStop...
切回MainActivity
03-25 10:15:17.549: D/MainActivity(5354): onRestart...
03-25 10:15:17.549: D/MainActivity(5354): onStart...
03-25 10:15:17.549: D/MainActivity(5354): onResume...
例子4:
进入MainActivity
03-25 10:16:05.329: D/MainActivity(5354): onCreate...
03-25 10:16:05.329: D/MainActivity(5354): onStart...
03-25 10:16:05.339: D/MainActivity(5354): onResume...
切到Home
03-25 10:16:18.059: D/MainActivity(5354): onSaveInstanceState...
03-25 10:16:18.059: D/MainActivity(5354): onPause...
03-25 10:16:18.199: D/MainActivity(5354): onStop...
切回MainActivity
03-25 10:16:34.649: D/MainActivity(5354): onRestart...
03-25 10:16:34.649: D/MainActivity(5354): onStart...
03-25 10:16:34.649: D/MainActivity(5354): onResume...
第一种情况,配置android:configChanges。
<activity
android:name="com.horsttnann.life.MainActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
例子1:
进入MainActivity
03-25 10:18:24.309: D/MainActivity(6086): onCreate...
03-25 10:18:24.309: D/MainActivity(6086): onStart...
03-25 10:18:24.319: D/MainActivity(6086): onResume...
竖屏切横屏
03-25 10:19:43.119: D/MainActivity(6282): onConfigurationChanged...
横屏切竖屏
03-25 10:20:14.069: D/MainActivity(6282): onConfigurationChanged...