Mengelola perubahan konfigurasi keyboard yang dapat dilepas
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Sistem Android memicu perubahan konfigurasi setiap kali keyboard
dipasang ke atau dilepas dari perangkat. Untuk memastikan pengalaman pengguna yang lancar dan
memaksimalkan produktivitas pengguna di perangkat layar besar dengan keyboard yang dapat dilepas,
aplikasi Anda harus mengelola perubahan konfigurasi keyboard secara efektif.
Mencegah pembuatan ulang aktivitas saat keyboard berubah
Untuk mencegah aktivitas Anda dibuat ulang saat keyboard yang dapat dilepas
dipasang atau dilepas, tambahkan nilai terkait keyboard ke atribut configChanges
manifes aplikasi Anda dan tambahkan tampilan ke hierarki tampilan aktivitas
sehingga aplikasi Anda dapat memproses perubahan konfigurasi.
1. Mendeklarasikan atribut configChanges
Perbarui elemen <activity> di manifes aplikasi dengan menambahkan
nilai keyboard|keyboardHidden ke daftar perubahan konfigurasi
yang telah dikelola:
Konten dan contoh kode di halaman ini tunduk kepada lisensi yang dijelaskan dalam Lisensi Konten. Java dan OpenJDK adalah merek dagang atau merek dagang terdaftar dari Oracle dan/atau afiliasinya.
Terakhir diperbarui pada 2025-02-06 UTC.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Informasi yang saya butuhkan tidak ada","missingTheInformationINeed","thumb-down"],["Terlalu rumit/langkahnya terlalu banyak","tooComplicatedTooManySteps","thumb-down"],["Sudah usang","outOfDate","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Masalah kode / contoh","samplesCodeIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 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)"]]