在Activity中添加了 android:configChanges属性,目的是当所指定属性(Configuration Changes)发生改变时,通知程序调用 onConfigurationChanged()函数
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
//TODO
domybusiness();
}
类别
通过设置这个属性可以使Activity捕捉设备状态变化,以下是可以被识别的内容:
CONFIG_FONT_SCALE
全局字体大小缩放发生改变CONFIG_MCC
MCC.移动国家号码,由三位数字组成,每个国家都有自己独立的MCC,可以识别手机用户所属国家CONFIG_MNC
MNC.移动网号,在一个国家或者地区中,用于区分手机用户的服务商CONFIG_LOCALE
用户所在地区发生变化CONFIG_TOUCHSCREEN
This should never normally happenCONFIG_KEYBOARD
键盘模式发生变化,例如:用户接入外部键盘输入CONFIG_NAVIGATION
This should never normally happen.CONFIG_ORIENTATION
设备旋转,横向显示和竖向显示模式切换。
实例
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidres.ConfigChangedTesting"
android:versionCode="1"
android:versionName="1.0.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ConfigChangedTesting"
android:label="@string/app_name"
android:configChanges="keyboardHidden|orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.view.View;
import android.widget.Button;
public class ConfigChangedTesting extends Activity {
/** Called when the activity is first created. */
static final int PICK_REQUEST = 1337;
Button viewButton=null;
Uri contact = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
setupViews();
}
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setupViews();
}
/* (non-Javadoc)
* @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
//super.onActivityResult(requestCode, resultCode, data);
if(requestCode == PICK_REQUEST){
if(resultCode==RESULT_OK){
contact = data.getData();
viewButton.setEnabled(true);
}
}
}
private void setupViews(){
setContentView(R.layout.main);
Button pickBtn = (Button)findViewById(R.id.pick);
pickBtn.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(Intent.ACTION_PICK,People.CONTENT_URI);
startActivityForResult(i,PICK_REQUEST);
}
});
viewButton =(Button)findViewById(R.id.view);
viewButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startActivity(new Intent(Intent.ACTION_VIEW, contact));
}
});
viewButton.setEnabled(contact!=null);
}
}
注意
不设置Activity的android:configChanges时,切屏会重新调用各个生命周期,切横屏时会执行一次,切竖屏时会执行两次
设置Activity的android:configChanges=”orientation”时,切屏还是会重新调用各个生命周期,切横、竖屏时只会执行一次
设置Activity的android: configChanges = “orientation | keyboardHidden”时,切屏不会重新调用各个生命周期,只会执行onConfigurationChanged方法
但是,自从Android 3.2(API 13),在设置Activity的android:configChanges=”orientation|keyboardHidden”后,还是一样会重新调用各个生命周期的。因为screen size也开始跟着设备的横竖切换而改变。所以,在AndroidManifest.xml里设置的MiniSdkVersion和 TargetSdkVersion属性大于等于13的情况下,如果你想阻止程序在运行时重新加载Activity,除了设置”orientation”,你还必须设置”ScreenSize”。
解决方法:
AndroidManifest.xml中设置android:configChanges=”orientation|screenSize“