[null,null,["最后更新时间 (UTC):2025-02-06。"],[],[],null,["# Manage detachable keyboard configuration changes\n\n\u003cbr /\u003e\n\nThe Android system triggers a configuration change every time a keyboard is\nattached to or detached from a device. To ensure a seamless user experience and\nmaximize user productivity on large screen devices with detachable keyboards,\nyour app needs to effectively manage keyboard configuration changes.\n\nPrevent activity recreation on keyboard change\n----------------------------------------------\n\nTo prevent your activity from being recreated when a detachable keyboard is\nattached or detached, add keyboard-related values to the `configChanges`\nattribute of your app manifest and add a view to the activity's view hierarchy\nso your app can listen for configuration changes.\n\n### 1. Declare the `configChanges` attribute\n\nUpdate the `\u003cactivity\u003e` element in the app manifest by adding the\n`keyboard|keyboardHidden` values to the list of already managed configuration\nchanges: \n\n \u003cactivity\n ...\n android:configChanges=\"...|keyboard|keyboardHidden\"\u003e\n\n### 2. Add an empty view to the view hierarchy\n\nDeclare a new view and add your handler code inside the view's\n`onConfigurationChanged()` method: \n\n### Kotlin\n\n```kotlin\nval v = object : View(this) {\n override fun onConfigurationChanged(newConfig: Configuration?) {\n super.onConfigurationChanged(newConfig)\n // Handler code here.\n }\n}\n```\n\n### Java\n\n```java\nView v = new View(this) {\n @Override\n protected void onConfigurationChanged(Configuration newConfig) {\n super.onConfigurationChanged(newConfig);\n // Handler code here.\n }\n};\n```\n| **Note:** Compose-based hierarchies require the addition of a view because the view's configuration handler is the only method that is always called for configuration changes.\n\nKey points\n----------\n\n- [`android:configChanges`](/guide/topics/manifest/activity-element#config): Attribute of the app manifest's `\u003cactivity\u003e` element. Informs the system about configuration changes the app manages.\n- [`View#onConfigurationChanged()`](/develop/ui/compose/quick-guides/content/(/reference/kotlin/android/view/View#onconfigurationchanged)) : Method that reacts to propagation of a new app configuration.\n\nResults\n-------\n\nYour app now responds to an external keyboard being attached or detached\nwithout recreating the running activity.\n\nCollections that contain this guide\n-----------------------------------\n\nThis guide is part of these curated Quick Guide collections that cover\nbroader Android development goals: \n\n### Optimize for large screens\n\nEnable your app to support an optimized user experience on tablets, foldables, and ChromeOS devices. \n[Quick guide collection](/quick-guides/collections/optimize-for-large-screens) \n\nHave questions or feedback\n--------------------------\n\nGo to our frequently asked questions page and learn about quick guides or reach out and let us know your thoughts. \n[Go to FAQ](/quick-guides/faq) [Leave feedback](https://issuetracker.google.com/issues/new?component=1573691&template=1993320)"]]