我有一个启动屏幕,该屏幕开始并持续5秒钟,然后消失并继续,或者被MainActivity替换,但是我想当我按下“后退”按钮并重新打开应用程序时,启动屏幕的加载速度比第一次打开时要快(<5秒),那么操作方法。
这是我的代码:
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()
}
答案 0 :(得分:1)
您可以使用静态变量,请尝试
companion object{
private var SPLASH_DELAY: Long = 5000
}
mDelayHandler!!.postDelayed(mRunnable, SPLASH_DELAY)
SPLASH_DELAY = 1000
答案 1 :(得分:0)
在调用startActivity()之前,您可能需要检查SplashActivity的生命周期。 另外,使用postDelayed()优于Thread.sleep()