在以前的版本中只要在AndroidManifest.xml文件中对activity指定android:configChanges="keyboardHidden|orientation"属性,转屏的时候就会不再重新调用OnCreate()函数,而是调用onConfigurationChanged()。

但是在自从android3.2以后,再这样设置的话,会发现转屏后仍然会调用OnCreate(),而不是onConfigurationChanged();跟踪framework层代码,就会发现问题所在,是由于google在android3.2中添加了screensize改变的通知,在转屏的时候,不仅是orientation发生了改变,screensize同样也发生了改变,而在判断是调用onConfigurationChanged还是OnCreate时,采用的是如下判断:

int diff = activity.mCurrentConfig.diff(config);

if (diff != 0) {

// If this activity doesn't handle any of the config changes then don't bother calling onConfigurationChanged as we'regoing to destroy it.

if ((~activity.mActivityInfo.getRealConfigChanged() & diff) == 0) {

shouldChangeConfig = true;

}

}

public int getRealConfigChanged() {

return applicationInfo.targetSdkVersion < android.os.Build.VERSION_CODES.HONEYCOMB_MR2 ? (configChanges | ActivityInfo.CONFIG_SCREEN_SIZE

| ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE) : configChanges;

}

通过上面的分析,可发现有两种方法解决该问题:(只需要修改AndroidManifest.xml)

1.指定android:configChanges="keyboardHidden|orientation|screenSize",其他的代码和以前的代码一样处理;

2.在AndroidManifest.xml中指定targetSdkVersion为3.2以前的版本(3.2的版本号为13),系统会自动加上screenSize属性值。

比如:<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="12" />

建议使用第一种方法

如下我写一个例子程序:

package com.configuration;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ConfigurationActivity extends Activity {

EditText et;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et = (EditText) findViewById(R.id.et);
Button bt = (Button) findViewById(R.id.bt);
bt.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
if(ConfigurationActivity.this.getRequestedOrientation() == -1){
Toast.makeText(ConfigurationActivity.this, "系统的屏幕方无法获取", Toast.LENGTH_LONG).show();
}else {
if(ConfigurationActivity.this.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
ConfigurationActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}else if(ConfigurationActivity.this.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
ConfigurationActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
}
});
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
Toast.makeText(this, "系统的屏幕方向发生改变", Toast.LENGTH_LONG).show();
switch (getRequestedOrientation()) {
case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
et.setText("当前屏幕朝向为:LANDSCAPE");
break;
case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
et.setText("当前屏幕朝向为:PORTRAIT");
break;
default:
break;
}
super.onConfigurationChanged(newConfig);
}
}

AndroidManifest.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.configuration"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".ConfigurationActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

更多相关文章

  1. 《Android 编程权威指南》(Android Programming The Big Nerd Ran
  2. 美团,大众点评,悬浮窗功能代码
  3. Android 第一行代码
  4. android 获取屏幕尺寸,密度等信息
  5. 单击Button 更改屏幕方向
  6. android获取版本信息、屏幕信息和设备编号

随机推荐

  1. android小功能实现之SQLite数据库的基本
  2. [Android实例] android多点触摸demo
  3. Android(安卓)红圈营销项目 —— 自定义
  4. Android叠加更新
  5. 【Android】如何实现一个简单的文件浏览
  6. android调用系统功能
  7. android study
  8. Suggestion: add 'tools:replace="androi
  9. Android(安卓)签名证书
  10. android 编译问题解决