I'm trying to display a screen for a few seconds when my app launches but I can't find how to do it in kotlin. I wanted that screen to be the first the user sees, then, after a few seconds, it would go directly to the login screen, can you help me?
Asked
Active
Viewed 536 times
0
-
2It's called a splash screen , [refer this](https://bignerdranch.com/blog/splash-screens-the-right-way/) – Anshul Sep 01 '21 at 04:11
-
1Check this answer where you no need to create different activity for splash screen: https://stackoverflow.com/questions/68864375/how-to-clear-previous-activity-on-android-without-start-new-activity/68864767#68864767 – Parmesh Sep 01 '21 at 04:41
-
1You can use a library like [Lottie](https://airbnb.io/lottie/#/README) for that purpose. And on this site (https://lottiefiles.com/)[https://lottiefiles.com/] you can download bunch of animation for your application. – Muhammad Farras Ma'ruf Sep 01 '21 at 04:50
2 Answers
0
please use handler to add some time delay to launch next functionality (in your case is launch next activity )
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
displayData();
}
}, 5000); //5000 is delay time you can change according to you need
-
Adding delay in the app for every launch is not a good idea, it's better practice to customise theme for splash screen. – Parmesh Sep 01 '21 at 04:35
-1
You can create a new Activity then set it as your starting activity like this:
<activity
android:name=".SplashScreenActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Then, in your SplashScreenActivity
, handle the delay like this:
Handler().postDelayed({
val intent = Intent(this, LoginActivity::class.java)
startActivity(intent)
finish()
}, 3000) // 3 seconds

danartillaga
- 1,349
- 1
- 8
- 19
-
This is also not a good practice to block main thread for few seconds. This will not give good user experience as user will wait for given seconds every time they launch app. – Parmesh Sep 01 '21 at 04:37