测试环境:
1. VIVO Y85,额,穷呀,android 8.1.0
2.夜神模拟器 Android 5.1.1
先上代码:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e("测试","onCreate");
}
@Override
protected void onRestart() {
super.onRestart();
Log.e("测试","onRestart");
}
@Override
protected void onStart() {
super.onStart();
Log.e("测试","onStart");
}
@Override
protected void onResume() {
super.onResume();
Log.e("测试","onResume");
}
@Override
protected void onPause() {
super.onPause();
Log.e("测试","onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.e("测试","onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.e("测试","onDestroy");
}
@Override
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.e("测试","onRestoreInstanceState----@NonNull Bundle savedInstanceState");
}
@Override
public void onRestoreInstanceState(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
super.onRestoreInstanceState(savedInstanceState, persistentState);
Log.e("测试","onRestoreInstanceState" +
"--@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState");
}
@Override
public void onSaveInstanceState(@NonNull Bundle outState, @NonNull PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
Log.e("测试","onSaveInstanceState----@NonNull Bundle outState, @NonNull PersistableBundle outPersistentState");
}
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
Log.e("测试","onSaveInstanceState----@NonNull Bundle outState");
}
@Override
public void onConfigurationChanged(@NonNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.e("测试","onConfigurationChanged");
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.screenchangeactivity">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
然后看Log输出:
一、正常的样子:(啥也没添加的时候)
1,横-》竖
2020-05-12 16:33:41.018 7180-7180/com.example.screenchangeactivity E/测试: onPause
2020-05-12 16:33:41.021 7180-7180/com.example.screenchangeactivity E/测试: onSaveInstanceState----@NonNull Bundle outState
2020-05-12 16:33:41.024 7180-7180/com.example.screenchangeactivity E/测试: onStop
2020-05-12 16:33:41.027 7180-7180/com.example.screenchangeactivity E/测试: onDestroy
2020-05-12 16:33:41.188 7180-7180/com.example.screenchangeactivity E/测试: onCreate
2020-05-12 16:33:41.192 7180-7180/com.example.screenchangeactivity E/测试: onStart
2020-05-12 16:33:41.193 7180-7180/com.example.screenchangeactivity E/测试: onRestoreInstanceState----@NonNull Bundle savedInstanceState
2020-05-12 16:33:41.200 7180-7180/com.example.screenchangeactivity E/测试: onResume
2.竖-》横
2020-05-12 16:35:15.381 7180-7180/com.example.screenchangeactivity E/测试: onPause
2020-05-12 16:35:15.388 7180-7180/com.example.screenchangeactivity E/测试: onSaveInstanceState----@NonNull Bundle outState
2020-05-12 16:35:15.391 7180-7180/com.example.screenchangeactivity E/测试: onStop
2020-05-12 16:35:15.394 7180-7180/com.example.screenchangeactivity E/测试: onDestroy
2020-05-12 16:35:15.588 7180-7180/com.example.screenchangeactivity E/测试: onCreate
2020-05-12 16:35:15.592 7180-7180/com.example.screenchangeactivity E/测试: onStart
2020-05-12 16:35:15.594 7180-7180/com.example.screenchangeactivity E/测试: onRestoreInstanceState----@NonNull Bundle savedInstanceState
2020-05-12 16:35:15.600 7180-7180/com.example.screenchangeactivity E/测试: onResume
看到没得,神特么切换竖屏后执行2次生命周期,明明只是在执行一次生命周期而已
二、.添加了
<activity android:name=".MainActivity" android:configChanges="orientation"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>,这个不知道咋搞的,反正复制过来就这个样子了,将就着看吧
然后就变这样了:
1.横-》竖,2.竖-》横跟“一“是一样的
三、添加了
<activity android:name=".MainActivity" android:configChanges="orientation|screenSize"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
1.横-》竖
05-12 17:21:23.411 4718-4718/com.example.screenchangeactivity E/测试: onConfigurationChanged
2.竖-》横
05-12 17:21:23.411 4718-4718/com.example.screenchangeactivity E/测试: onConfigurationChanged
总结:
1.如果没有添加 android:configChanges="orientation|screenSize"的话,则会重新走次生命周期方法,而且系统会调用onSaveInstanceState来保存当前Activity的状态。这个方法的调用是在onStop之前,它和onPause没有既定的时序关系,该方法只在Activity被异常终止的情况下调用。当异常终止的Activity被重建以后,系统会调用onRestoreInstanceState,并且把Activity销毁时onSaveInstanceState方法所保存的Bundle对象参数同时传递给onRestoreInstanceState和onCreate方法
2.如果添加了android:configChanges="orientation|screenSize"的话,则切换屏幕不会重新走生命周期方法,只会执行 @Override
public void onConfigurationChanged(@NonNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.e("测试","onConfigurationChanged");
},这个方法