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. linux和windows平台下下载android sdk的源代码【Z】
  2. Android的消息机制,用Android线程间通信的Message机制,Android中Ha
  3. Android获取本机电话号码的简单方法
  4. 如何在eclipse的android工程里引用android sdk之外的类和方法
  5. android 在配置文件中指定上级activity
  6. android studio在模拟器上的中文乱码问题解决方法
  7. Android5.x+ 格式化外部存储(u盘, sdcard)的方法

随机推荐

  1. Android与JavaScript之间的相互调用
  2. 如何在后台运行Linux命令?
  3. Android原码结构分析
  4. Android(安卓)5.1 Audio HAL分析
  5. Android 进行单元测试难在哪-序
  6. 一款常用的 Squid 日志分析工具
  7. android 应用在SD卡创建应用的文件夹
  8. Android进程与线程
  9. Android 文件系统的结构
  10. 简析为安卓开发者服务的Google官方Androi