با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
WebView یک مؤلفه متداول است که یک سیستم پیشرفته برای مدیریت حالت ارائه می دهد. یک WebView باید وضعیت خود را حفظ کند و موقعیت خود را در سراسر تغییرات پیکربندی حرکت دهد. وقتی کاربر دستگاه را میچرخاند یا یک تلفن تاشو باز میکند، WebView میتواند موقعیت اسکرول را از دست بدهد، که کاربر را مجبور میکند دوباره از بالای WebView به موقعیت اسکرول قبلی حرکت کند.
WebView در مدیریت وضعیت خود خوب است. شما می توانید با مدیریت هر چه بیشتر تغییرات پیکربندی برای به حداقل رساندن تعداد دفعات ایجاد مجدد WebView از این کیفیت بهره ببرید. برنامه شما باید تغییرات پیکربندی را انجام دهد زیرا بازآفرینی فعالیت (روش سیستم برای مدیریت تغییرات پیکربندی) WebView دوباره ایجاد میکند که باعث از بین رفتن حالت WebView میشود.
مدیریت دولت
در طول تغییرات پیکربندی تا حد امکان از بازآفرینی Activity خودداری کنید و اجازه دهید WebView باطل شود تا بتواند با حفظ حالت خود اندازه آن را تغییر دهد.
برای مدیریت وضعیت WebView :
تغییرات پیکربندی انجام شده توسط برنامه شما را اعلام کنید
حالت WebView را باطل کنید
1. تغییرات پیکربندی را به فایل AndroidManifest.xml برنامه خود اضافه کنید
با مشخص کردن تغییرات پیکربندی که توسط برنامه شما (به جای سیستم) انجام می شود، از فعالیت تفریحی خودداری کنید:
این مرحله فقط برای سیستم view اعمال می شود، زیرا Jetpack Compose برای تغییر اندازه عناصر Composable به درستی نیازی به بی اعتبار کردن چیزی ندارد. با این حال، Compose اغلب اگر به درستی مدیریت نشود، یک WebView دوباره ایجاد می کند.
نکات کلیدی
android:configChanges : ویژگی عنصر <activity> مانیفست. تغییرات پیکربندی انجام شده توسط فعالیت را فهرست می کند.
View#invalidate() : روشی که باعث میشود یک view دوباره ترسیم شود. توسط WebView به ارث رسیده است.
نتایج
مؤلفههای WebView برنامه شما اکنون وضعیت خود را حفظ کرده و موقعیت خود را در چندین تغییر پیکربندی، از تغییر اندازه گرفته تا تغییر جهت، تا کردن و باز کردن دستگاه حفظ میکنند.
مجموعه هایی که حاوی این راهنما هستند
این راهنما بخشی از مجموعههای راهنمای Quick Guide است که اهداف توسعه Android گستردهتری را پوشش میدهد:
بهینه سازی برای نمایشگرهای بزرگ
برنامه خود را فعال کنید تا از تجربه کاربری بهینه شده در رایانه لوحی، دستگاههای تاشو و دستگاههای ChromeOS پشتیبانی کند.
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-02-06 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","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 بهوقت ساعت هماهنگ جهانی."],[],[],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)"]]