我有MainActivity
and LoginActivity
,MainActivity
使用Theme.MySplash
(有蓝色和应用程序图标)。
在第一次启动时,SplashScreen 显示良好(带有背景和应用程序图标),但是,当我重新启动MainActivity
fromLoginActivity
时,SplashScreen 显示没有应用程序图标。我重新启动MainActivity
而不是返回,MainActivity
因为在我的真实应用程序中,我需要重新创建MainActivity
这里是演示代码
样式.xml
<style name="Theme.MySplash" parent="Theme.SplashScreen">
<item name="windowSplashScreenBackground">#00f</item>
<item name="windowSplashScreenAnimatedIcon">@drawable/ic_launcher_foreground</item>
<item name="postSplashScreenTheme">@style/Theme.AndroidSplashScreen12</item>
</style>
<style name="Theme.AndroidSplashScreen12" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<item name="android:windowBackground">#fff</item>
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
</style>
主要活动
class MainActivity : AppCompatActivity() {
var keepSplashScreen = true
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val splashScreen = installSplashScreen()
setContentView(R.layout.activity_main)
splashScreen.setKeepVisibleCondition { keepSplashScreen }
Handler(Looper.getMainLooper()).postDelayed({
keepSplashScreen = false
}, 1500)
findViewById<Button>(R.id.button_login).setOnClickListener {
startActivity(Intent(this@MainActivity, LoginActivity::class.java))
}
}
}
登录活动
class LoginActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login2)
findViewById<Button>(R.id.button_start_main).setOnClickListener {
finishAffinity()
startActivity(Intent(this@LoginActivity, MainActivity::class.java))
}
}
}