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

使用SplashScreen API后如何跳过SplashActivity跳转目标页面

解决SplashScreen消失后直接跳转目标页面的问题

问题出在你给SplashActivity设置了独立布局setContentView(R.layout.splash),导致系统SplashScreen消失后,会先显示这个布局,再完成跳转逻辑。要实现跳过SplashActivity直接跳转,需要让系统SplashScreen保持显示直到所有判断完成,同时不加载SplashActivity的布局。

具体修改步骤:

  1. 移除SplashActivity的布局加载:删掉setContentView(R.layout.splash),因为系统SplashScreen已经负责启动页展示,不需要额外布局。
  2. 让SplashScreen保持显示直到判断完成:使用SplashScreen.setKeepOnScreenCondition()方法,通过一个布尔变量控制启动页是否持续显示。
  3. 判断完成后关闭启动页并跳转:在所有跳转逻辑执行前,更新布尔变量让SplashScreen自动消失,再启动目标页面。

修改后的完整代码:

public class SplashActivity extends AppCompatActivity {
    private FirebaseAuth auth;
    private RequestQueue requestQueue;
    private String apiUrl = "你的维护接口地址";
    // 控制SplashScreen是否保持显示的标志位
    private boolean keepSplashOn = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 初始化SplashScreen并设置保持显示的条件
        SplashScreen splashScreen = SplashScreen.installSplashScreen(this);
        splashScreen.setKeepOnScreenCondition(() -> keepSplashOn);
        
        super.onCreate(savedInstanceState);
        // 移除setContentView(R.layout.splash)
        FirebaseApp.initializeApp(this);
        initialize();
        checkMaintenanceAndRedirect();
    }

    private void initialize() {
        auth = FirebaseAuth.getInstance();
        requestQueue = Volley.newRequestQueue(this);
    }

    private void checkMaintenanceAndRedirect() {
        StringRequest stringRequest = new StringRequest(
                Request.Method.GET,
                apiUrl,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonResponse = new JSONObject(response);
                            String status = jsonResponse.getString("status");
                            if ("off".equals(status)) {
                                navigateToMainteActivity();
                            } else {
                                redirectToAppropriateActivity();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            navigateToMainteActivity();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(getApplicationContext(), "Error occurred!", Toast.LENGTH_SHORT).show();
                        navigateToMainteActivity();
                    }
                }
        );
        requestQueue.add(stringRequest);
    }

    private void redirectToAppropriateActivity() {
        FirebaseUser currentUser = auth.getCurrentUser();
        if (currentUser != null) {
            navigateToHomeActivity();
        } else {
            navigateToOnboardActivity();
        }
    }

    private void navigateToHomeActivity() {
        keepSplashOn = false; // 通知SplashScreen可以消失了
        Intent ii = new Intent(getApplicationContext(), HomeActivity.class);
        startActivity(ii);
        finish();
    }

    private void navigateToOnboardActivity() {
        keepSplashOn = false;
        Intent ii = new Intent(getApplicationContext(), OnboardActivity.class);
        startActivity(ii);
        finish();
    }

    private void navigateToMainteActivity() {
        keepSplashOn = false;
        Intent im = new Intent(getApplicationContext(), MainteActivity.class);
        startActivity(im);
        finish();
    }
}

关键代码解释:

  • setKeepOnScreenCondition(() -> keepSplashOn):这个方法会让系统SplashScreen一直显示,直到keepSplashOn变为false。这样就不会出现中间显示SplashActivity布局的情况。
  • 移除setContentView:因为SplashActivity不需要自己的UI,所有启动页展示都由系统SplashScreen负责。
  • 跳转前设置keepSplashOn = false:触发SplashScreen自动消失,同时立即启动目标Activity,实现无缝跳转。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 23:43:15