android 转屏 onConfigurationChanged 不会执行的问题
2024-08-22 11:32:30
参考 http://blog.csdn.net/tangzhilu/article/details/7399988
MainActivity 代码
package com.example.configchangesample; import android.os.Bundle;
import android.app.Activity;
import android.content.res.Configuration;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView; public class MainActivity extends Activity { TextView textView1;
String TAG = "configchangesample"; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); textView1 = (TextView)this.findViewById(com.example.configchangesample.R.id.textView1);
textView1.setText("init");
Log.i(TAG, "onCreate");
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
textView1.setText("onConfigurationChanged");
Log.i(TAG, "onConfigurationChanged:" + newConfig.orientation); if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
textView1.setText("ORIENTATION_LANDSCAPE");
} else { textView1.setText("ORIENTATION_PORTRAIT");
} }
}
如果是 如下的配置
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.configchangesample"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.configchangesample.MainActivity"
android:label="@string/app_name"
android:configChanges="orientation|screenSize" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>
在android 4.2.2 真机 和 android 2.3.5 真机验证,onConfigurationChanged 会被执行。
如果 android:configChanges="orientation|screenSize" 改为 android:configChanges="orientation"
在android 4.2.2 真机上验证,onConfigurationChanged 不会被执行。
在android 2.3.5 真机上验证,onConfigurationChanged 会被执行。
去掉 android:targetSdkVersion="18" 并且 android:configChanges="orientation"
在android 4.2.2 真机 和 android 2.3.5 真机验证,onConfigurationChanged 会被执行。
另外,
1.记得在手机上打开自动转屏的开关
2.测试发现, 转屏时,无论如何 onCreate 不会被触发。
网上的资料 加上keyboardHidden后onCreate 不会被触发,不加就会触发,这个解释貌似站不住脚。
3. 反正记得一点,如果设置了android:targetSdkVersion ,记得加上screenSize
解释比较罗嗦,点击这里查看 http://developer.android.com/guide/topics/manifest/activity-element.html
最新文章
- BZOJ 1227: [SDOI2009]虔诚的墓主人
- 《C标准库》——之<;string.h>;
- PHP插入header(&#39;content-type:text/html;charset=";utf-8&#39;)和error_reporting()
- OS X open finder here in terminal
- Java笔记——JavaMail发送邮件
- 《A Tour of PostgreSQL Internals》学习笔记——系统表和数据类型
- Chapter 5 Convert Image Set To LevelDB/LMDB
- list笔记总结
- 定位表的数据块并且dump出来
- html 动态添加TABLE的行。
- java学习笔记_GUI(3)
- Screen命令安装使用教程
- 微信小程序开发02-小程序基本介绍
- UnzipUtil
- C#使用Emit构造拦截器动态代理类
- gradle-4.1-all.zip离线包下载 极速 android studio2.3 3.0编译必备
- openstack学习-nove计算节点部署(五)
- 动态加载JS脚本到HTML
- 富文本编辑器&;FileReader
- ios开发之--复制到剪切板