防止在Android中重新加载WebView
我有一个带有标题文本视图的片段和顶部的图像视图,然后在底部进行WebView。
该片段正在主要活动中的某种底部导航中使用NAVHOSTRAGMENT和带有标签的Navgraph导航。
现在,当最初转到此选项卡时,WebView按预期加载,但随后我导航到另一个选项卡并返回到WebView选项卡,重新创建了片段,因此WebView再次加载了以前显示的内容的状态。 因此,当我返回此选项卡时,我希望WebView不要再次重新加载,而是要显示先前显示的内容。
我该如何实现?
我尝试保存和恢复实例状态,但这并不能按照我从文档中理解的内容正常工作。
我也没有ViewPager,因此无法调整屏幕页面限制。
还有其他选择可以将片段留在后台而不会在返回时重新加载的情况下?
一个想法是创建一个首次调用时实例化的WebView的一个实例,每当我返回该页面时,我都会在布局中以编程方式设置该WebView。这是实现这一目标的正确方法吗?
fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/titleBar"
android:layout_width="wrap_content"
android:layout_height="?android:attr/actionBarSize"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/iv_profile"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginStart="@dimen/layout_distance_medium"
android:gravity="center_vertical"
android:background="?android:attr/windowBackground"
style="@style/H6"
app:layout_constraintHorizontal_bias="0.0" />
<com.google.android.material.imageview.ShapeableImageView
android:id="@+id/iv_profile"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginEnd="@dimen/layout_distance_medium"
android:src="@drawable/ic_someguy"
android:scaleType="centerCrop"
android:layout_gravity="end|center_vertical"
app:shapeAppearanceOverlay="@style/Circle"
app:layout_collapseMode="pin" />
<WebView
android:id="@+id/webview"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/titleBar"
app:layout_constraintBottom_toBottomOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
main_activity.xml
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?attr/tabBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:labelVisibilityMode="labeled"
app:menu="@menu/bottom_nav_menu" />
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/bottom_navigation"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:defaultNavHost="true"
app:navGraph="@navigation/mobile_navigation" />
I have a fragment with a title text view and an image view in the top, followed by a webview in the bottom.
This fragment is being navigated to and away from using a navhostfragment and a navGraph with tabs in some kind of bottom navigation in the main activity.
Now when initially go to this tab, the webview loads as intended but then I navigate away to another tab and return to the webview tab the fragment gets recreated and thus the webview loads again loosing the state of what was shown before.
So when I return to this tab I would want the webview not to reload again but show the previously shown contents.
How can I achieve that?
I tried saving and restoring the instance state but that did not work as expected by what I understood from the documentation.
I also do not have a viewpager so I can not adapt offscreen page limit.
Are there any other options of maybe leaving the fragment in the background without it reloading when returning?
One idea would be to create a single instance of that webview thats instantiated when first being called and whenever I return to that page I set that webview programmatically in the layout. Is that the correct way of achieving this?
fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/titleBar"
android:layout_width="wrap_content"
android:layout_height="?android:attr/actionBarSize"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/iv_profile"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginStart="@dimen/layout_distance_medium"
android:gravity="center_vertical"
android:background="?android:attr/windowBackground"
style="@style/H6"
app:layout_constraintHorizontal_bias="0.0" />
<com.google.android.material.imageview.ShapeableImageView
android:id="@+id/iv_profile"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginEnd="@dimen/layout_distance_medium"
android:src="@drawable/ic_someguy"
android:scaleType="centerCrop"
android:layout_gravity="end|center_vertical"
app:shapeAppearanceOverlay="@style/Circle"
app:layout_collapseMode="pin" />
<WebView
android:id="@+id/webview"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/titleBar"
app:layout_constraintBottom_toBottomOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
main_activity.xml
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?attr/tabBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:labelVisibilityMode="labeled"
app:menu="@menu/bottom_nav_menu" />
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/bottom_navigation"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:defaultNavHost="true"
app:navGraph="@navigation/mobile_navigation" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论