参考 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

最新文章

  1. BZOJ 1227: [SDOI2009]虔诚的墓主人
  2. 《C标准库》——之&lt;string.h&gt;
  3. PHP插入header(&#39;content-type:text/html;charset=&quot;utf-8&#39;)和error_reporting()
  4. OS X open finder here in terminal
  5. Java笔记——JavaMail发送邮件
  6. 《A Tour of PostgreSQL Internals》学习笔记——系统表和数据类型
  7. Chapter 5 Convert Image Set To LevelDB/LMDB
  8. list笔记总结
  9. 定位表的数据块并且dump出来
  10. html 动态添加TABLE的行。
  11. java学习笔记_GUI(3)
  12. Screen命令安装使用教程
  13. 微信小程序开发02-小程序基本介绍
  14. UnzipUtil
  15. C#使用Emit构造拦截器动态代理类
  16. gradle-4.1-all.zip离线包下载 极速 android studio2.3 3.0编译必备
  17. openstack学习-nove计算节点部署(五)
  18. 动态加载JS脚本到HTML
  19. 富文本编辑器&amp;FileReader
  20. ios开发之--复制到剪切板

热门文章

  1. Linux watchdog 关闭退出功能
  2. [转]Java中的POJO类
  3. python -修改文件中某一行
  4. this inspection reports usage of the default file template for file header
  5. 快速掌握Vue.js使用【转】
  6. jquery 实现下拉菜单
  7. javascript完美实现图片拖动改变顺序
  8. 函数式编程——C#理解
  9. 在kali linux之下安装wps之后 报错字体缺失
  10. 关于Unity中Vector2和Vector3的使用