I have a splash screen that starts and lasts for 5 seconds, then disappears and continues or is replaced with MainActivity, but I want when I press the back button and reopen the application, the splash screen will load faster than at first open (<5s), so how to do it.
This my code:
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()
}