@@ -14,12 +14,15 @@ import androidx.core.view.WindowCompat
1414import androidx.core.view.WindowInsetsCompat
1515import androidx.core.view.WindowInsetsControllerCompat
1616import android.view.KeyEvent
17+ import androidx.activity.viewModels
1718import androidx.lifecycle.viewmodel.compose.viewModel
1819import com.hotbell.radio.ui.theme.HotBellTheme
1920import com.hotbell.radio.ui.theme.PitchBlack
2021
2122class WakeUpActivity : ComponentActivity () {
2223
24+ private val viewModel: WakeUpViewModel by viewModels()
25+
2326 override fun onCreate (savedInstanceState : Bundle ? ) {
2427 super .onCreate(savedInstanceState)
2528
@@ -37,8 +40,6 @@ class WakeUpActivity : ComponentActivity() {
3740 val targetPhotoPath = intent.getStringExtra(" EXTRA_TARGET_PHOTO_PATH" )
3841
3942 setContent {
40- val viewModel: WakeUpViewModel = viewModel()
41-
4243 // Trigger playback on launch
4344 androidx.compose.runtime.LaunchedEffect (Unit ) {
4445 viewModel.startAlarm(this @WakeUpActivity, stationUuid, stationName, stationUrl)
@@ -118,4 +119,14 @@ class WakeUpActivity : ComponentActivity() {
118119 else -> super .onKeyUp(keyCode, event)
119120 }
120121 }
122+
123+ override fun onUserLeaveHint () {
124+ super .onUserLeaveHint()
125+ if (viewModel.isAlarmActive.value) {
126+ // The user pressed Home or Recent Apps. Bring the activity back to the front immediately.
127+ val i = android.content.Intent (this , WakeUpActivity ::class .java)
128+ i.addFlags(android.content.Intent .FLAG_ACTIVITY_NEW_TASK or android.content.Intent .FLAG_ACTIVITY_SINGLE_TOP )
129+ startActivity(i)
130+ }
131+ }
121132}
0 commit comments