fix: 页内跳转支持返回键逐级返回
RemoteUIScreen 增加返回栈:页内跳转时压栈,BackHandler 在栈非空时先回上一页, 栈空后才交回系统(退出应用 / 关闭 Activity)。
This commit is contained in:
1 parent
0b27edcbea
commit
8cc8107222
1 file changed
+14
-1
+14
-1
@@ -3,6 +3,7 @@ package com.ttstd.composeremote.ui
|
|||||||
import android.annotation.SuppressLint
|
import android.annotation.SuppressLint
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
|
import androidx.activity.compose.BackHandler
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
import androidx.compose.foundation.layout.Box
|
import androidx.compose.foundation.layout.Box
|
||||||
import androidx.compose.foundation.layout.Column
|
import androidx.compose.foundation.layout.Column
|
||||||
@@ -240,9 +241,16 @@ fun RemoteUIScreen(
|
|||||||
onOpenActivity: ((String) -> Unit)? = null,
|
onOpenActivity: ((String) -> Unit)? = null,
|
||||||
) {
|
) {
|
||||||
var page by rememberSaveable { mutableStateOf(startPage) }
|
var page by rememberSaveable { mutableStateOf(startPage) }
|
||||||
|
// 页内跳转的返回栈:按返回键先回上一页,栈空时才交给系统退出。
|
||||||
|
var backStack by rememberSaveable { mutableStateOf(listOf<String>()) }
|
||||||
var state by remember { mutableStateOf<RemoteUiState>(RemoteUiState.Loading) }
|
var state by remember { mutableStateOf<RemoteUiState>(RemoteUiState.Loading) }
|
||||||
var retryToken by remember { mutableIntStateOf(0) }
|
var retryToken by remember { mutableIntStateOf(0) }
|
||||||
|
|
||||||
|
BackHandler(enabled = backStack.isNotEmpty()) {
|
||||||
|
page = backStack.last()
|
||||||
|
backStack = backStack.dropLast(1)
|
||||||
|
}
|
||||||
|
|
||||||
LaunchedEffect(page, retryToken) {
|
LaunchedEffect(page, retryToken) {
|
||||||
// 命中字节缓存时不闪 loading,也不会重复请求。
|
// 命中字节缓存时不闪 loading,也不会重复请求。
|
||||||
if (RemoteDocumentStore.get(page) == null) {
|
if (RemoteDocumentStore.get(page) == null) {
|
||||||
@@ -259,7 +267,12 @@ fun RemoteUIScreen(
|
|||||||
Box(modifier = Modifier.fillMaxSize()) {
|
Box(modifier = Modifier.fillMaxSize()) {
|
||||||
RemoteDocumentView(
|
RemoteDocumentView(
|
||||||
document = (state as? RemoteUiState.Success)?.document,
|
document = (state as? RemoteUiState.Success)?.document,
|
||||||
onNavigate = { target -> page = target },
|
onNavigate = { target ->
|
||||||
|
if (target != page) {
|
||||||
|
backStack = backStack + page
|
||||||
|
page = target
|
||||||
|
}
|
||||||
|
},
|
||||||
onOpenActivity = onOpenActivity,
|
onOpenActivity = onOpenActivity,
|
||||||
modifier = Modifier.fillMaxSize()
|
modifier = Modifier.fillMaxSize()
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in new issue
Block a user