Quản lý các thay đổi về cấu hình bàn phím có thể tháo rời
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Hệ thống Android sẽ kích hoạt thay đổi cấu hình mỗi khi bàn phím được gắn vào hoặc tháo khỏi thiết bị. Để đảm bảo trải nghiệm người dùng liền mạch và tối đa hoá năng suất của người dùng trên các thiết bị màn hình lớn có bàn phím có thể tháo rời, ứng dụng của bạn cần quản lý hiệu quả các thay đổi về cấu hình bàn phím.
Ngăn chặn việc tạo lại hoạt động khi thay đổi bàn phím
Để ngăn hoạt động của bạn được tạo lại khi bàn phím có thể tháo rời được gắn hoặc tháo rời, hãy thêm các giá trị liên quan đến bàn phím vào thuộc tính configChanges của tệp kê khai ứng dụng và thêm một thành phần hiển thị vào hệ phân cấp khung hiển thị của hoạt động để ứng dụng của bạn có thể theo dõi các thay đổi về cấu hình.
1. Khai báo thuộc tính configChanges
Cập nhật phần tử <activity> trong tệp kê khai ứng dụng bằng cách thêm các giá trị keyboard|keyboardHidden vào danh sách các thay đổi về cấu hình đã được quản lý:
android:configChanges: Thuộc tính của phần tử <activity> trong tệp kê khai ứng dụng. Thông báo cho hệ thống về các thay đổi về cấu hình mà ứng dụng quản lý.
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-02-06 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-02-06 UTC."],[],[],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)"]]