Android display Splash-Screen while loading

前端 未结 6 511
忘掉有多难
忘掉有多难 2020-12-02 17:38

I have an Android App, which shows a \"Splash Screen\" for 3 seconds. After that, the MainActivity gets loaded.

Unfortunately the MainActivity takes additional ~4 se

6条回答
  •  借酒劲吻你
    2020-12-02 18:17

    Just do like in this article: https://www.bignerdranch.com/blog/splash-screens-the-right-way/

    1 - create a XML layout like this for the splash screen. I called it "background_splash.xml"

    
    
    
        
    
        
            
        
    
    
    

    2 - Then, go to the styles.xml and write a style like this:

    
    

    3 - Write an activity to your splash. I called it SplashActivity.kt

    package com.example.kissmoney
    
    import android.content.Intent
    import android.os.Bundle
    import androidx.appcompat.app.AppCompatActivity
    
    
    class SplashActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
            finish()
        }
    }
    

    4 - Finally, go to you AndroidManifest.xml and add you activity splash: (Note: don't remove nothing in the AndroidManifest, just add this befora the activity Main).

    
           
                
                
           
    
    

    This is done. You don't need to worry about the time that your application will demand to start, the splash will be there just for enough time. When you MainActivity is ready, it will be showed.

提交回复
热议问题