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" />