android:configChanges

Lists configuration changes that the activity will handle itself. When a configuration change occurs at runtime, the activity is shut down and restarted by default, but declaring a configuration with this attribute will prevent the activity from being restarted. Instead, the activity remains running and itsonConfigurationChanged()method is called.


http://developer.android.com/guide/topics/manifest/activity-element.html


以下引用写的很不错的网文:http://www.cnblogs.com/and_he/archive/2011/05/24/2055087.html


一般在AndroidManifest.xml文件中都没有使用到android:configChanges="keyboardHidden|orientation"配置,当然还是很有用的哈

就是如果配置了这个属性,当我们横竖屏切换的时候会直接调用onCreate方法中的onConfigurationChanged方法,而不会重新执行onCreate方法,那当然如果不配置这个属性的话就会重新调用onCreate方法了,下面是测试

AndroidManifest.xml文件

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

< application android:icon ="@drawable/icon" android:label ="@string/app_name" >
< activity android:name =".TestActivity"
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 >

main.xml文件

                                    <?                    xml version="1.0" encoding="utf-8"                    ?>                    
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:orientation
="vertical"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
>
< TextView
android:id ="@+id/tv"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:text
="横竖屏切换测试"
/>
< EditText
android:layout_width ="fill_parent"
android:layout_height
="wrap_content"
android:id
="@+id/et"
/>
</ LinearLayout >

TestActivity.java文件

                                    package                     com.test;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

public class TestActivity extends Activity {
EditText et;
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.main);
et
= (EditText) findViewById(R.id.et);
tv
= (TextView) findViewById(R.id.tv);
System.out.println(
" 我是onCreate方法 " );
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super .onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
tv.setText(
" 横屏 " );
}
else {
tv.setText(
" 竖屏 " );
}

}
}
可以亲测一下,以验证我刚才说的结论“ 当在xml文件中配置了android:configChanges="keyboardHidden|orientation"属性,在横竖屏切换的时候不会重新执行Activity的onCreate方法,只是执行onConfigurationChanged方法,否则切换的时候会重新执行onCreate方法


更多相关文章

  1. Android的消息机制,用Android线程间通信的Message机制,Android中Ha
  2. android的编译和运行过程深入分析
  3. Android自定义视图二:用Canvas和Paint绘制折线图
  4. android中的坐标系以及获取坐标的方法
  5. android自学笔记 开始--->第一个应用--->第一个项目
  6. Android(安卓)事件流详解之View事件分发
  7. Android初步(开发环境的搭建)
  8. 如何在eclipse的android工程里引用android sdk之外的类和方法
  9. 深入探讨 Android(安卓)传感器

随机推荐

  1. Android(安卓)Gradle配置资源前缀
  2. Android(安卓)如何获取当前Activity实例
  3. Android(安卓)Studio3.5 xml文件格式化(R
  4. Android基础笔记(十一)- Service基础和注意
  5. Android中利用反射机制来控制AlertDialog
  6. Android(安卓)组件之一(Service)
  7. Android获取WIFI的BSSID遇到的坑(已解决)
  8. Android(安卓)Adobe XMP与JPEG
  9. Android异步处理机制AsyncTask的理解
  10. 【Android】Android(安卓)BLE开发