Home > Blockchain >  is there a way to show splash screen until the webview fragment finish loading?
is there a way to show splash screen until the webview fragment finish loading?

Time:01-05

I have a MainActivity that installSplashScreen() at first, then it will load the WebView, the webviewFragment is in other file, I am trying to use splashScreen.setKeepOnScreenCondition{ } to keep splash screen showing until the webviewFragment reach to onPageFinished() state, how am I supposed to do that?

CodePudding user response:

There are several alternatives to communicate both components:

  1. you may use a listener in your fragment that notifies the activity,
  2. you may cast the activity from fragment context to notify it,
  3. you may use the activity ViewModel and observer pattern to notify the change
  4. singletons though not recommended
  5. event bus implementations
  6. send a broadcast and receive it with a BroadCastListener in your splash or MainActivity.

CodePudding user response:

You can use a layout like this

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
        android:id="@ id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@ id/cl_container_splash"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</FrameLayout>
  • Frame Layout (Parent)
    • WebView (visibility = gone)
    • Splash Screen (visible)

Now load your url, and in your onPageFinished() call following.

binding.webView.visibility = View.Visible
binding.root.removeView(binding.clContainerSplash)
  • Related