android - Activity 在 onCreate 之后调用 onStop
问题描述
我有 3 项活动:入职活动、登录活动和第三项活动。当用户第一次打开我的应用程序时,它会在登录活动后进入引导,它会要求使用 Firebase 登录谷歌。当用户这样做时,它将开始第三个活动并完成旧的活动,如下所示:
val intent = Intent(baseContext, SkyActivity::class.java)
startActivity(intent)
finish()
但是每次用户登录并启动3rd activity
,onPause()
都会被调用。它使应用程序崩溃,因为我onPause()
停止了我的背景歌曲:
override fun onPause() {
super.onPause()
if (backgroundSongService.isPlaying()) {
backgroundSongService.pauseMusic()
}
}
而此时,我backgroundSongService
的仍然是null
(我在我的 onCreate 中调用了一个初始化它的函数)。所以我得到了错误:
lateinit property backgroundSongService has not been initialized
但这只发生在用户登录时。如果他在登录后关闭应用程序并再次打开,他仍然会登录。所以活动流程将是:OnboardingActivity
->3rdActivity
OnboardingActivity 将像这样调用第三个活动:
if (firebaseUser != null) {
val intent = Intent(baseContext, SkyActivity::class.java)
startActivity(intent)
finish()
}
和我Intent
里面的一模一样LoginActivity
。但不同的是,这一次,onPause
不会在第三个活动中调用。为什么它只在之后调用LoginActivity
?
解决方案
快速解决您的崩溃问题
使用前验证后台服务不为空
override fun onPause() {
super.onPause()
if (backgroundSongService!=null && backgroundSongService.isPlaying()) {
backgroundSongService.pauseMusic()
}
}
推荐阅读
- javascript - 将值传递给 React map 函数中的函数
- java - 如何使用java字符串方法返回选定数量的字符?
- android - 如何制作类似 Play 商店和 Gmail 的应用栏
- php - 有没有办法在 node.js 中使用 GMP 库来使用 SRP6 进行凭据
- javascript - 在堆栈标签内添加总计数分组堆积柱形图-Highcharts
- sprite-kit - SpriteKit:如何创建与设备屏幕大小相同的区域?
- terraform - 将 terraform 模块迁移到更新的提供程序格式
- wordpress - 清除新映像上的 Kubernetes 持久卷
- html - 有没有办法防止 Anchor 标签成为弹性项目?
- java - 你可以使用 JUnit 来比较两个 String ArrayLists 吗?