1.android:configChanges="keyboardHidden|orientation"

<activity android:name=".TwitterActivity$AppListFragment"    android:configChanges="keyboardHidden|orientation"  />

If you use this,then android won't restart activity when screen is rotated.Instead,onConfigurationChanged(Configuration newConfig) will be call.So you can do something in it.

e.g:

if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){      setContentView(R.layout.file_list_landscape);  }else{      setContentView(R.layout.file_list);  }

Quick Background

By default, when certain key configuration changes happen on Android (a common example is an orientation change), Android fully restarts the running Activity to help it adjust to such changes.

When you defineandroid:configChanges="keyboardHidden|orientation"in your AndroidManifest, you are telling Android: "Please don't do the default reset when the keyboard is pulled out, or the phone is rotated; I want to handle this myself. Yes, I know what I'm doing"

Is this a good thing? We shall soon see...

No worries?

One of the pros you start with is that there is:

no need to worry about your activity been rotated

In many cases, people mistakenly believe that when they have an error that is being generated by an orientation change ("rotation"), they can simply fix it by putting inandroid:configChanges="keyboardHidden|orientation".

However, android:configChanges="keyboardHidden|orientation" is nothing more than a bandaid. In truth, there are many ways a configuration change can be triggered. For example, if the user selects a new language (i.e. the locale has changed), your activity will be restarted in the same way it does by an orientation change. If you want you can viewa list of all the different types of config changes.

Edit: More importantly, though, ashackbodpoints out in the comments, your activity will also be restarted when your app is in the background and Android decides to free up some memory by killing it. When the user comes back to your app, Android will attempt to restart the activity in the same way it does if there was some other configuration change. If you can't handle that - the user will not be happy...

In other words, usingandroid:configChanges="keyboardHidden|orientation"is not a solution for your "worries." The right way is to code your activities so that they are happy with any restart Android throws at them. This is a good practice that will help you down the road, so get used to it.

So when should I use it?

As you mentioned there is a distinct advantage. Overwriting the default configuration change for a rotation by handling it yourself will speed things up. However, this speed does come with a price of convenience.

To put it simply, if you use the same layout for both portrait and landscape you're in good shape by doing the overwrite. Instead of a full-blown reload of the activity, the views will simply shift around to fill the remaining space.

However, if for some reason you use a different layout when the device is in landscape, the fact that Android reloads your Activity is good because it will then load up the correct layout. [If you use the override on such an Activity, and want to do some magical re-layout at runtime... well, good luck - it's far from simple]

Quick Summary

By all means, ifandroid:configChanges="keyboardHidden|orientation"is right for you, then use it. ButPLEASEbe sure to test what happens when something changes, because an orientation change is not the only way a full Acitivity restart can be triggered.

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. SpringBoot项目通用功能及框架知识点介绍
  2. 深入理解JVM—JVM内存模型
  3. mymes部署虚拟机安装以及使用Linux,看一
  4. 学习Linux的第7天
  5. SpringBoot实现POI报表操作
  6. 深入浅出Java并发包—CountDownLauch原理
  7. SpringBoot结合POI百万级数据报表操作
  8. 把GOF(俗称四人帮)当作圣经
  9. Java集合LinkedList源码剖析
  10. Java面试题全集