You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

实现WebView加载完成前显示启动页(Splash Screen)

解决启动页跳转后WebView白屏问题

你的问题核心是固定延迟3秒跳转和WebView加载耗时不匹配,导致跳转后WebView未加载完成出现白屏。要实现启动页直到WebView加载完成再关闭,可按以下步骤修改:

方案思路

  1. 启动页(SplashActivity)直接启动MainActivity,但不立即关闭自身
  2. MainActivity中监听WebView的加载状态,加载完成后通知启动页关闭
  3. 增加超时兜底机制,避免极端情况下启动页一直显示

步骤1:修改SplashActivity代码

去掉固定延迟的线程,改为启动MainActivity并通过广播接收加载完成的通知,同时设置最长等待时间:

public class SplashActivity extends AppCompatActivity {
    private BroadcastReceiver finishReceiver;
    private Handler timeoutHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_splash);

        // 注册广播接收器,监听WebView加载完成通知
        finishReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if ("com.example.WEBVIEW_LOADED".equals(intent.getAction())) {
                    finish();
                    // 收到通知后取消超时任务
                    if (timeoutHandler != null) {
                        timeoutHandler.removeCallbacksAndMessages(null);
                    }
                }
            }
        };
        IntentFilter filter = new IntentFilter("com.example.WEBVIEW_LOADED");
        registerReceiver(finishReceiver, filter);

        // 启动MainActivity,不关闭当前启动页
        startActivity(new Intent(this, MainActivity.class));

        // 设置5秒超时兜底,避免WebView加载卡住
        timeoutHandler = new Handler(Looper.getMainLooper());
        timeoutHandler.postDelayed(() -> {
            finish();
        }, 5000);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 注销广播和移除超时任务,防止内存泄漏
        if (finishReceiver != null) {
            unregisterReceiver(finishReceiver);
        }
        if (timeoutHandler != null) {
            timeoutHandler.removeCallbacksAndMessages(null);
        }
    }
}

步骤2:修改MainActivity代码

给WebView设置WebViewClient,在页面加载完成(或加载失败)时发送广播通知启动页关闭:

public class MainActivity extends AppCompatActivity {
    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webView = findViewById(R.id.webview);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true); // 根据你的网页需求决定是否开启JS

        webView.setWebViewClient(new WebViewClient() {
            // 页面加载完成时发送通知
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                sendFinishBroadcast();
            }

            // 页面加载失败时也发送通知,避免启动页一直显示
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                sendFinishBroadcast();
                // 可选:这里可以显示加载错误的提示页面
            }
        });

        // 替换成你的目标网页地址
        webView.loadUrl("https://your-target-url.com");
    }

    // 封装发送广播的方法
    private void sendFinishBroadcast() {
        Intent intent = new Intent("com.example.WEBVIEW_LOADED");
        sendBroadcast(intent);
    }
}

关键注意事项

  • 广播Action字符串(com.example.WEBVIEW_LOADED)要保持一致,建议定义成常量避免拼写错误
  • 必须在SplashActivity销毁时注销广播接收器,否则会导致内存泄漏
  • 超时兜底时间可根据你的网页实际加载速度调整,推荐3-5秒
  • 如果你的WebView需要加载本地HTML,逻辑同样适用,只需修改loadUrl为loadData或loadDataWithBaseURL

内容的提问来源于stack exchange,提问作者sandev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 03:51:06