2011年6月8日水曜日

向きなど変更時のアクティビティ再スタート防止

画面の向き等が変更された場合、Activityが一旦終了して再スタートされますよ、とのことみたいです。
ログを出してみると、向き変更時にonCreateが呼ばれていました。
 
 
向きを固定にしてしまえば問題ないのですが、固定したくないときもあります。
そんな時は、AndroidManifest.xmlのactivityにandroid:configChanges属性を追加することで回避できます。
 
<activity 
    android:name=".ActivityName" 
    android:label="@string/app_name" 
    android:configChanges="orientation|keyboardHidden"
回避されるのは設定されているコンフィグレーションが変更された場合に限ります。
 
またこの設定をした場合、アクティビティが再スタートされる代わりにonConfigurationChangedが呼ばれることになります。
変更時に何か処理をしたい場合はここに記述することになります。
 
※詳細はデベロッパーサイトで確認ください。
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 its onConfigurationChanged() method is called.
Note: Using this attribute should be avoided and used only as a last-resort. Please read Handling Runtime Changes for more information about how to properly handle a restart due to a configuration change.
Any or all of the following strings are valid values for this attribute. Multiple values are separated by '|' — for example, "locale|navigation|orientation".
Value Description
"mcc" The IMSI mobile country code (MCC) has changed — a SIM has been detected and updated the MCC.
"mnc" The IMSI mobile network code (MNC) has changed — a SIM has been detected and updated the MNC.
"locale" The locale has changed — the user has selected a new language that text should be displayed in.
"touchscreen" The touchscreen has changed. (This should never normally happen.)
"keyboard" The keyboard type has changed — for example, the user has plugged in an external keyboard.
"keyboardHidden" The keyboard accessibility has changed — for example, the user has revealed the hardware keyboard.
"navigation" The navigation type (trackball/dpad) has changed. (This should never normally happen.)
"orientation" The screen orientation has changed — the user has rotated the device.
"screenLayout" The screen layout has changed — this might be caused by a different display being activated.
"fontScale" The font scaling factor has changed — the user has selected a new global font size.
"uiMode" The user interface mode has changed — this can be caused when the user places the device into a desk/car dock or when the the night mode changes. See UiModeManager. Introduced in API Level 8.
All of these configuration changes can impact the resource values seen by the application. Therefore, whenonConfigurationChanged() is called, it will generally be necessary to again retrieve all resources (including view layouts, drawables, and so on) to correctly handle the change.
 
 

0 件のコメント:

コメントを投稿