Wear UI: intercept system back so Messages/Conversation/Accounts pop within app

Add BackHandler in WearApp: back from thread: -> messages, messages/accounts
-> dialer. At the root dialer route the handler is disabled so back finishes
the Activity normally. Fixes back-from-Messages dropping to the clock face.
This commit is contained in:
2026-08-21 04:09:41 +01:00
parent 100ee241a2
commit f0a4fd18cd

View File

@ -8,6 +8,7 @@ import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.view.KeyEvent import android.view.KeyEvent
import androidx.activity.ComponentActivity import androidx.activity.ComponentActivity
import androidx.activity.compose.BackHandler
import androidx.activity.compose.setContent import androidx.activity.compose.setContent
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.layout.* import androidx.compose.foundation.layout.*
@ -189,6 +190,17 @@ class MainActivity : ComponentActivity() {
fun WearApp(activity: MainActivity) { fun WearApp(activity: MainActivity) {
// Show the in-call screen whenever there is an active or incoming call. // Show the in-call screen whenever there is an active or incoming call.
val hasCall by remember { derivedStateOf { CallState.calls.isNotEmpty() } } val hasCall by remember { derivedStateOf { CallState.calls.isNotEmpty() } }
// Intercept the system back gesture so navigation stays inside the app
// (Messages/Conversation/Accounts screens pop to their parent instead of
// finishing the Activity and dropping to the clock face). At the root
// "dialer" route the handler is disabled, so back behaves normally.
BackHandler(enabled = activity.route.value != "dialer") {
activity.route.value = when {
activity.route.value.startsWith("thread:") -> "messages"
activity.route.value == "messages" || activity.route.value == "accounts" -> "dialer"
else -> "dialer"
}
}
MaterialTheme { MaterialTheme {
Scaffold(timeText = { TimeText() }) { Scaffold(timeText = { TimeText() }) {
when { when {