背景介绍:
在编写android小应用的时候,碰到了这样的一个问题:当推开手机的实体键盘时,屏幕由竖屏转换为横屏,此时应用程序的显示界面(Activity)就会被销毁了,这个让人比较郁闷。
如何才能让这个activity不被销毁呢?
------------------------------------- 背景分割线 ---------------------------------------------
资料查询:
在android开发网上有这么几段话:
If the configuration of the device (as defined by the Resources.Configuration
class) changes, then anything displaying a user interface will need to update to match that configuration. Because Activity is the primary mechanism for interacting with the user, it includes special support for handling configuration changes.
Unless you specify otherwise, a configuration change (such as a change in screen orientation, language, input devices, etc) will cause your current activity to be destroyed, going through the normal activity lifecycle process of onPause()
, onStop()
, and onDestroy()
as appropriate. If the activity had been in the foreground or visible to the user, once onDestroy()
is called in that instance then a new instance of the activity will be created, with whatever savedInstanceState the previous instance had generated from onSaveInstanceState(Bundle)
.
In some special cases, you may want to bypass restarting of your activity based on one or more types of configuration changes. This is done with the android:configChanges
attribute in its manifest. For any types of configuration changes you say that you handle there, you will receive a call to your current activity's onConfigurationChanged(Configuration)
method instead of being restarted. If a configuration change involves any that you do not handle, however, the activity will still be restarted and onConfigurationChanged(Configuration)
will not be called.
To declare that your Activity handles a configuration change, edit the appropriate <activity>
element in your manifest file to include the android:configChanges
attribute with a string value that represents the configuration that you want to handle. Possible values are listed in the documentation for the