在Android Kotlin中重新访问应用程序后快速加载启动屏幕



我有一个启动屏幕,它启动并持续5秒,然后消失并继续,或者被MainActivity取代,但我希望当我按下后退按钮并重新打开应用程序时,启动屏幕的加载速度会比第一次打开时快(<5s(,那么该怎么做呢?

这是我的代码:

class SplashActivity : BaseActivity() {
companion object{
private const val PREFS:String = "prefs"
private const val FIRST_RUN:String = "first_run"
private const val SPLASH_DELAY: Long = 5000
}

private var mDelayHandler: Handler? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_splash)
mDelayHandler = Handler()
mDelayHandler!!.postDelayed(mRunnable, SPLASH_DELAY)
var animation: Animation = AnimationUtils.loadAnimation(this,R.anim.splash_animation)
logo.startAnimation(animation)
}
private val mRunnable = Runnable {
Thread(Runnable {
try {
Thread.sleep(100)
} catch (e: InterruptedException) {
e.printStackTrace()
}
startActivity()
}).start()
}
private fun startActivity() {
val settings = getSharedPreferences(PREFS, 0)
val firstRun = settings.getBoolean(FIRST_RUN, false)
var intent = if (!firstRun) {
val editor = settings.edit()
editor.putBoolean(FIRST_RUN, true)
editor.commit()
Intent(this, OnBoardingActivity::class.java)
} else {
Intent(this, MainActivity::class.java)
}
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
startActivity(intent)
this.finish()
mDelayHandler!!.removeCallbacks(mRunnable)
}
override fun onDestroy() {
mDelayHandler?.removeCallbacks(mRunnable)
super.onDestroy()
}

您可以使用静态变量,试试这个

companion object{
private var SPLASH_DELAY: Long = 5000
}
mDelayHandler!!.postDelayed(mRunnable, SPLASH_DELAY)
SPLASH_DELAY = 1000

在调用startActivity((之前,您可能需要检查SplashActivity的生命周期。此外,使用postDelayed((比Thread.sleep((更好

最新更新