面试题:Activity横竖屏切换时的生命周期如何变化
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了面试题:Activity横竖屏切换时的生命周期如何变化相关的知识,希望对你有一定的参考价值。
参考技术A 看视频时经常用到横竖屏切换时,你有没有了解过,这时候的activity生命周期如何变换?切换横竖屏监听变化需要用到activity的属性:configChanges属性,用于监听屏幕的横竖屏切换事件。android:configChanges,用于捕获手机状态的改变,当横竖屏切换,屏幕尺寸变化,弹出键盘,系统设置改变等条件,就会触发回调事件onConfigurationChanged。
mcc:移动国家号码,由三位数字组成,每个国家都有自己独立的MCC,可以识别手机用户所属国家;
mnc:移动网号,在一个国家或者地区中,用于区分手机用户的服务商;
locale:用户所在地区发生变化;
touchscreen:触摸屏幕变化;
keyboard:键盘模式发生变化,例如:用户接入外部键盘输入;
keyboardHidden:用户打开手机硬件键盘;
navigation:导航类型改变;
orientation:设备旋转,横向显示和竖向显示模式切换;
fontScale:全局字体大小缩放发生改变。
我这个activity不是main所以不设置<intent-filter>属性,如果是首发,其他不用修改,加上android:configChanges="orientation|keyboard|layoutDirection|screenSize">即可。
此时属于第四种结果,无法在logcat中显示生命周期变化情况,只执行重写的onConfigurationChanged方法。
刚开始竖屏调用方法顺序:
==>>onStart()
==>>onResume()
变为横屏时的生命周期变化:
==>>onPause()
==>>onStop()
==>>onDestory() //调用完整生命周期一次
==>>onStart()
==>>onResume()
重新变为竖屏时的生命周期变化:
==>>onPause()
==>>onStop()
==>>onDestory() //调用完整生命周期二次
==>>onStart()
==>>onResume()
退出activity时生命周期变化:
==>>onPause()
==>>onStop()
==>>onDestory() //调用完整生命周期三次
总结:横屏调用完整生命周期一次,竖屏调用两次。
总结:也是横屏调用完整生命周期1次,竖屏调用2次。
总结:也是横屏调用完整生命周期1次,竖屏调用2次。
总结:横竖屏切换时不会调用各个生命周期方法。只会执行onConfigurationChanged方法,弹出Toast消息提示用户。
Activity生命周期-切换横竖屏-Android
创建一个项目,测试横竖屏切换的时候生命周期的变换
ScreenActivity.java
public class ScreenActivity extends AppCompatActivity
private static final String TAG = "ScreenActivity";
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen);
Log.i(TAG, "onCreate: ");
@Override
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState)
super.onRestoreInstanceState(savedInstanceState);
Log.i(TAG, "onRestoreInstanceState: ");
@Override
protected void onStart()
super.onStart();
Log.i(TAG, "onStart: ");
@Override
protected void onSaveInstanceState(Bundle outState)
super.onSaveInstanceState(outState);
Log.i(TAG, "onSaveInstanceState: ");
@Override
protected void onResume()
super.onResume();
Log.i(TAG, "onResume: ");
@Override
protected void onStop()
super.onStop();
Log.i(TAG, "onStop: ");
@Override
protected void onPause()
super.onPause();
Log.i(TAG, "onPause: ");
@Override
protected void onDestroy()
super.onDestroy();
Log.i(TAG, "onDestroy: ");
@Override
public void onConfigurationChanged(Configuration newConfig)
super.onConfigurationChanged(newConfig);
Log.i(TAG, "onConfigurationChanged: ");
一、不修改AndroidManifest.xml
1、第一次创建
I/ScreenActivity: onCreate:
I/ScreenActivity: onStart:
I/ScreenActivity: onResume:
2、切换横屏
I/ScreenActivity: onPause:
I/ScreenActivity: onStop:
I/ScreenActivity: onSaveInstanceState:
I/ScreenActivity: onDestroy:
I/ScreenActivity: onCreate:
I/ScreenActivity: onStart:
I/ScreenActivity: onRestoreInstanceState:
onResume:
3、切换竖屏
I/ScreenActivity: onPause:
I/ScreenActivity: onStop:
I/ScreenActivity: onSaveInstanceState:
I/ScreenActivity: onDestroy:
I/ScreenActivity: onCreate:
I/ScreenActivity: onStart:
onRestoreInstanceState:
onResume:
二、修改AndroidManifest.xml
- orientation 屏幕在纵向和横向间旋转
- keyboardHidden 键盘显示或隐藏
- screenSize 屏幕大小改变了
1、android:configChanges=“orientation”
1.1、第一次创建
I/ScreenActivity: onCreate:
I/ScreenActivity: onStart:
I/ScreenActivity: onResume:
1.2、切换横屏
I/ScreenActivity: onPause:
I/ScreenActivity: onStop:
I/ScreenActivity: onSaveInstanceState:
I/ScreenActivity: onDestroy:
I/ScreenActivity: onCreate:
I/ScreenActivity: onStart:
I/ScreenActivity: onRestoreInstanceState:
onResume:
1.3、切换竖屏
I/ScreenActivity: onPause:
I/ScreenActivity: onStop:
I/ScreenActivity: onSaveInstanceState:
I/ScreenActivity: onDestroy:
I/ScreenActivity: onCreate:
I/ScreenActivity: onStart:
onRestoreInstanceState:
onResume:
2、android:configChanges=“orientation|screenSize”
2.1、第一次创建
I/ScreenActivity: onCreate:
I/ScreenActivity: onStart:
I/ScreenActivity: onResume:
2.2、切换横屏
I/ScreenActivity: onConfigurationChanged:
2.3、切换竖屏
I/ScreenActivity: onConfigurationChanged:
3、android:configChanges=“orientation|screenSize|keyboardHidden”
3.1、第一次创建
I/ScreenActivity: onCreate:
I/ScreenActivity: onStart:
I/ScreenActivity: onResume:
3.2、切换横屏
I/ScreenActivity: onConfigurationChanged:
3.3、切换竖屏
I/ScreenActivity: onConfigurationChanged:
以上是关于面试题:Activity横竖屏切换时的生命周期如何变化的主要内容,如果未能解决你的问题,请参考以下文章