新创建一个Activity,用来此次测试。
先贴代码
package com.hugo.apj.activitylifetest; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("TAG","OnCreate");
} @Override
protected void onStart() {
Log.i("TAG","OnStart");
super.onStart();
} @Override
protected void onPause() {
Log.i("TAG","OnPause");
super.onPause();
} @Override
protected void onStop() {
Log.i("TAG","OnStop");
super.onStop();
} @Override
protected void onRestart() {
Log.i("TAG","OnRestart");
super.onRestart();
} @Override
protected void onResume() {
Log.i("TAG","OnResume");
super.onResume();
} @Override
protected void onDestroy() {
Log.i("TAG","OnDestroy");
super.onDestroy();
}
}
以下为测试结果:
第一次运行出现:
08-05 15:56:48.282 20772-20772/com.hugo.apj.activitylifetest I/TAG: OnCreate
08-05 15:56:48.283 20772-20772/com.hugo.apj.activitylifetest I/TAG: OnStart
08-05 15:56:48.283 20772-20772/com.hugo.apj.activitylifetest I/TAG: OnResume
先清除Logcat,从竖屏转换到横屏:
08-05 16:01:01.363 20772-20772/com.hugo.apj.activitylifetest I/TAG: OnPause
08-05 16:01:01.364 20772-20772/com.hugo.apj.activitylifetest I/TAG: OnStop
08-05 16:01:01.364 20772-20772/com.hugo.apj.activitylifetest I/TAG: OnDestroy
08-05 16:01:01.429 20772-20772/com.hugo.apj.activitylifetest I/TAG: OnCreate
08-05 16:01:01.429 20772-20772/com.hugo.apj.activitylifetest I/TAG: OnStart
08-05 16:01:01.430 20772-20772/com.hugo.apj.activitylifetest I/TAG: OnResume
同样清除Logcat,再从横屏转换到竖屏:
08-05 16:01:37.330 20772-20772/com.hugo.apj.activitylifetest I/TAG: OnPause
08-05 16:01:37.333 20772-20772/com.hugo.apj.activitylifetest I/TAG: OnStop
08-05 16:01:37.333 20772-20772/com.hugo.apj.activitylifetest I/TAG: OnDestroy
08-05 16:01:37.399 20772-20772/com.hugo.apj.activitylifetest I/TAG: OnCreate
08-05 16:01:37.399 20772-20772/com.hugo.apj.activitylifetest I/TAG: OnStart
08-05 16:01:37.400 20772-20772/com.hugo.apj.activitylifetest I/TAG: OnResume
从以上结果可以发现,在不讨论添加其他限制代码的情况下,横竖屏转换都会重新走完除了OnRestart外的其他六个生命周期。