Android Studio之横竖屏切换时Activity注意事项
1、打开 Android Studio,新建一个Empty Activity,具体如下图



2、然后在MainAcitivity中添加生命周期函数的打印信息,便于后期官场Activity的情况,具体如下图


3、连接设备或者使用虚拟器,编译运行,切换横竖屏,很显然,默认情况下,Activity 存在销毁在重建的情况,具体如下图


4、回到 Android Studio ,在 AndroidManifest中的MainActivity中添加 android:configChanges="keyboardHidden|screenSize|orientation" 属性 ,使得 MainActivity 在 横竖屏切换(还包括 键盘隐藏和屏幕尺寸变化)时不受影响,具体如下图

5、AndroidManifest.xml 的具体内容如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.xan.activitylifetest">
<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"
android:configChanges="keyboardHidden|screenSize|orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity
</application>
</manifest>
6、再次编译运行,设备无论怎么横竖屏切换,Activity 都没有在销毁重建,符合了使用,具体如下图


声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
阅读量:65
阅读量:153
阅读量:162
阅读量:49
阅读量:124