WebView는 일반적으로 사용되는 구성요소로, 상태 관리를 위한 고급 시스템을 제공합니다. WebView는 구성 변경 시 상태와 스크롤 위치를 유지해야 합니다. WebView는 사용자가 기기를 회전하거나 폴더블 휴대전화를 펼칠 때 스크롤하던 위치를 잃을 수 있습니다. 이런 경우 사용자가 WebView의 상단에서 이전 스크롤 위치로 다시 스크롤해야 합니다.
WebView는 상태를 관리하는 데 능숙합니다. 가능한 한 많은 구성 변경을 관리하여 WebView가 다시 생성되는 횟수를 최소화하면 이 기능의 장점을 활용할 수 있습니다. 활동 재생성 (시스템에서 구성 변경을 처리하는 방식) 시 WebView도 다시 생성되므로 WebView에서 상태가 손실될 수 있기 때문에 앱이 구성 변경을 처리해야 합니다.
상태 관리
구성 변경 중에 Activity 재생성을 최대한 방지하고 상태를 유지하면서 크기를 조절할 수 있도록 WebView를 무효화합니다.
이 페이지에 나와 있는 콘텐츠와 코드 샘플에는 콘텐츠 라이선스에서 설명하는 라이선스가 적용됩니다. 자바 및 OpenJDK는 Oracle 및 Oracle 계열사의 상표 또는 등록 상표입니다.
최종 업데이트: 2025-02-06(UTC)
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["필요한 정보가 없음","missingTheInformationINeed","thumb-down"],["너무 복잡함/단계 수가 너무 많음","tooComplicatedTooManySteps","thumb-down"],["오래됨","outOfDate","thumb-down"],["번역 문제","translationIssue","thumb-down"],["샘플/코드 문제","samplesCodeIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-02-06(UTC)"],[],[],null,["# Manage WebView state\n\n[`WebView`](/reference/kotlin/android/webkit/WebView) is a commonly used\ncomponent that offers an advanced system for state management. A `WebView`\nmust maintain its state and scroll position across configuration changes. A\n`WebView` can lose scroll position when the user rotates the device or unfolds\na foldable phone, which forces the user to scroll again from the top of the\n`WebView` to the previous scroll position.\n\n`WebView` is good at managing its state. You can take advantage of this quality\nby managing as many configuration changes as possible to minimize the number of\ntimes a `WebView` is recreated. Your app should handle configuration changes\nbecause activity recreation (the system's way of handling configuration\nchanges) recreates the `WebView`, which causes the `WebView` to lose state.\n\nManage state\n------------\n\nAvoid [`Activity`](/reference/kotlin/android/app/Activity) recreation as much as\npossible during configuration changes, and let the `WebView` invalidate so it\ncan resize while retaining its state.\n\nTo manage `WebView` state:\n\n- Declare configuration changes handled by your app\n- Invalidate the `WebView` state\n\n#### 1. Add configuration changes to your app's `AndroidManifest.xml` file\n\nAvoid activity recreation by specifying the configuration changes handled by\nyour app (rather than by the system): \n\n \u003cactivity\n android:name=\".MyActivity\"\n android:configChanges=\"screenLayout|orientation|screenSize\n |keyboard|keyboardHidden|smallestScreenSize\" /\u003e\n\n| **Note:** While this nonexhaustive list of configuration changes might be okay for many applications, make sure to manage the configuration changes that make the most sense for your case based on how your users interact with the app, for how long, and when. To find the best combination of configurations for your app, see [`android:configChanges`](/guide/topics/manifest/activity-element#config).\n\n#### 2. Invalidate `WebView` whenever your app receives a configuration change\n\n### Kotlin\n\n```kotlin\noverride fun onConfigurationChanged(newConfig: Configuration) {\n super.onConfigurationChanged(newConfig)\n webView.invalidate()\n}\n```\n\n### Java\n\n```java\n@Override\npublic void onConfigurationChanged(@NonNull Configuration newConfig) {\n super.onConfigurationChanged(newConfig);\n webview.invalidate();\n}\n```\n\nThis step applies only to the view system, as Jetpack Compose does not need to\ninvalidate anything to resize\n[`Composable`](/reference/kotlin/androidx/compose/runtime/Composable) elements\ncorrectly. However, Compose recreates a `WebView` often if not managed\ncorrectly.\n\nKey points\n----------\n\n- [`android:configChanges`](/guide/topics/manifest/activity-element#config): Attribute of the manifest `\u003cactivity\u003e` element. Lists the configuration changes handled by the activity.\n- [`View#invalidate()`](/reference/kotlin/android/view/View#invalidate_2): Method that causes a view to be redrawn. Inherited by `WebView`.\n\nResults\n-------\n\nYour app's `WebView` components now retain their state and scroll position\nacross multiple configuration changes, from resizing to orientation changes\nto device folding and unfolding.\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)"]]