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

如何在Kotlin开发的应用启动时展示数秒启动页后跳转至登录页

Kotlin 安卓应用启动页实现方案

1. 配置启动页为应用首入口

首先在AndroidManifest.xml中,将启动页Activity设置为应用启动项,移除原有主Activity/登录Activity的LAUNCHER意图过滤器:

<activity
    android:name=".SplashActivity"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<!-- 登录页配置示例 -->
<activity
    android:name=".LoginActivity"
    android:exported="false" />

2. 编写启动页布局

在res/layout/activity_splash.xml中编写启动页的UI内容,可根据需求自定义Logo、品牌名等元素:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:gravity="center"
    android:orientation="vertical">

    <ImageView
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:src="@mipmap/ic_launcher"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:textSize="18sp"
        android:layout_marginTop="16dp"/>

</LinearLayout>

3. 实现延迟跳转逻辑

推荐使用协程实现延迟,生命周期自动绑定更稳妥,不会出现内存泄漏风险:

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class SplashActivity : AppCompatActivity() {
    // 自定义启动页停留时长,单位为毫秒,示例为3秒
    private val SPLASH_DELAY = 3000L

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)

        lifecycleScope.launch {
            delay(SPLASH_DELAY)
            // 跳转至登录页
            startActivity(Intent(this@SplashActivity, LoginActivity::class.java))
            // 关闭启动页,避免用户按返回键退回该页面
            finish()
        }
    }
}

若项目未引入协程,也可以用Handler实现相同逻辑:

import android.os.Handler
import android.os.Looper

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_splash)

    Handler(Looper.getMainLooper()).postDelayed({
        startActivity(Intent(this@SplashActivity, LoginActivity::class.java))
        finish()
    }, SPLASH_DELAY)
}

注意事项

  • 不要在启动页中加入额外耗时操作,避免启动等待时间过长影响用户体验
  • 跳转完成后必须调用finish()关闭启动页,防止用户按下返回键退回到启动页
  • 如需适配Android 12及以上系统自带的启动页规范,可额外配置系统启动页样式,避免出现双重启动页的问题

内容的提问来源于stack exchange,提问作者Marina R Albuquerque

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 22:48:04