Skip to content

Commit 10e9f09

Browse files
authored
Merge pull request #152 from android/dt/back-button-visibility
Show how to control the visibility of a back button on the detail pane
2 parents 8e16c9d + a1e7ee7 commit 10e9f09

File tree

4 files changed

+61
-23
lines changed

4 files changed

+61
-23
lines changed

app/src/main/java/com/example/nav3recipes/scenes/listdetail/Content.kt

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import androidx.compose.foundation.background
2020
import androidx.compose.foundation.clickable
2121
import androidx.compose.foundation.layout.Arrangement
2222
import androidx.compose.foundation.layout.Column
23+
import androidx.compose.foundation.layout.Box
2324
import androidx.compose.foundation.layout.Spacer
2425
import androidx.compose.foundation.layout.fillMaxSize
2526
import androidx.compose.foundation.layout.fillMaxWidth
@@ -31,6 +32,10 @@ import androidx.compose.material3.ListItem
3132
import androidx.compose.material3.ListItemDefaults
3233
import androidx.compose.material3.MaterialTheme
3334
import androidx.compose.material3.Text
35+
import androidx.compose.material.icons.Icons
36+
import androidx.compose.material.icons.automirrored.filled.ArrowBack
37+
import androidx.compose.material3.Icon
38+
import androidx.compose.material3.IconButton
3439
import androidx.compose.runtime.Composable
3540
import androidx.compose.ui.Alignment
3641
import androidx.compose.ui.Modifier
@@ -73,24 +78,41 @@ fun ConversationListScreen(
7378
@Composable
7479
fun ConversationDetailScreen(
7580
conversationDetail: ConversationDetail,
81+
onBack: () -> Unit,
7682
onProfileClicked: () -> Unit
7783
) {
78-
Column(
84+
Box(
7985
modifier = Modifier
8086
.fillMaxSize()
8187
.background(colors[conversationDetail.colorId])
82-
.padding(16.dp),
83-
horizontalAlignment = Alignment.CenterHorizontally,
84-
verticalArrangement = Arrangement.Center
88+
.padding(16.dp)
8589
) {
86-
Text(
87-
text = "Conversation Detail Screen: ${conversationDetail.id}",
88-
style = MaterialTheme.typography.headlineMedium,
89-
color = MaterialTheme.colorScheme.onSurface
90-
)
91-
Spacer(modifier = Modifier.height(16.dp))
92-
Button(onClick = onProfileClicked) {
93-
Text("View Profile")
90+
if (LocalBackButtonVisibility.current) {
91+
IconButton(
92+
onClick = onBack,
93+
modifier = Modifier.align(Alignment.TopStart)
94+
) {
95+
Icon(
96+
imageVector = Icons.AutoMirrored.Filled.ArrowBack,
97+
contentDescription = "Back"
98+
)
99+
}
100+
}
101+
Column(
102+
modifier = Modifier
103+
.fillMaxSize(),
104+
horizontalAlignment = Alignment.CenterHorizontally,
105+
verticalArrangement = Arrangement.Center
106+
) {
107+
Text(
108+
text = "Conversation Detail Screen: ${conversationDetail.id}",
109+
style = MaterialTheme.typography.headlineMedium,
110+
color = MaterialTheme.colorScheme.onSurface
111+
)
112+
Spacer(modifier = Modifier.height(16.dp))
113+
Button(onClick = onProfileClicked) {
114+
Text("View Profile")
115+
}
94116
}
95117
}
96118
}

app/src/main/java/com/example/nav3recipes/scenes/listdetail/ListDetailActivity.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class ListDetailActivity : ComponentActivity() {
8787
) { conversationDetail ->
8888
ConversationDetailScreen(
8989
conversationDetail = conversationDetail,
90+
onBack = { backStack.removeLastOrNull() },
9091
onProfileClicked = { backStack.add(Profile) }
9192
)
9293
}

app/src/main/java/com/example/nav3recipes/scenes/listdetail/ListDetailScene.kt

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import androidx.compose.foundation.layout.Row
2525
import androidx.compose.foundation.layout.fillMaxSize
2626
import androidx.compose.material3.adaptive.currentWindowAdaptiveInfo
2727
import androidx.compose.runtime.Composable
28+
import androidx.compose.runtime.CompositionLocalProvider
29+
import androidx.compose.runtime.compositionLocalOf
2830
import androidx.compose.runtime.remember
2931
import androidx.compose.ui.Modifier
3032
import androidx.navigation3.runtime.NavEntry
@@ -52,18 +54,22 @@ class ListDetailScene<T : Any>(
5254
Column(modifier = Modifier.weight(0.4f)) {
5355
listEntry.Content()
5456
}
55-
Column(modifier = Modifier.weight(0.6f)) {
56-
AnimatedContent(
57-
targetState = detailEntry,
58-
contentKey = { entry -> entry.contentKey },
59-
transitionSpec = {
60-
slideInHorizontally(
61-
initialOffsetX = { it }
62-
) togetherWith
63-
slideOutHorizontally(targetOffsetX = { -it })
57+
58+
// Let the detail entry know not to display a back button.
59+
CompositionLocalProvider(LocalBackButtonVisibility provides false){
60+
Column(modifier = Modifier.weight(0.6f)) {
61+
AnimatedContent(
62+
targetState = detailEntry,
63+
contentKey = { entry -> entry.contentKey },
64+
transitionSpec = {
65+
slideInHorizontally(
66+
initialOffsetX = { it }
67+
) togetherWith
68+
slideOutHorizontally(targetOffsetX = { -it })
69+
}
70+
) { entry ->
71+
entry.Content()
6472
}
65-
){ entry ->
66-
entry.Content()
6773
}
6874
}
6975
}
@@ -87,6 +93,13 @@ class ListDetailScene<T : Any>(
8793
}
8894
}
8995

96+
/**
97+
* This `CompositionLocal` can be used by a detail `NavEntry` to decide whether to display
98+
* a back button. Default is `true`. It is set to `false` for a detail `NavEntry` when being
99+
* displayed in a `ListDetailScene`.
100+
*/
101+
val LocalBackButtonVisibility = compositionLocalOf{ true }
102+
90103
@Composable
91104
fun <T : Any> rememberListDetailSceneStrategy(): ListDetailSceneStrategy<T> {
92105
val windowSizeClass = currentWindowAdaptiveInfo().windowSizeClass

app/src/main/java/com/example/nav3recipes/scenes/listdetail/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ A `ListDetailSceneStrategy` will return a `ListDetailScene` if:
88
- A `Detail` entry is the last item in the back stack
99
- A `List` entry is in the back stack
1010

11+
The `ListDetailScene` provides a `CompositionLocal` named `LocalBackButtonVisibility` that can be used by the detail `NavEntry` to control whether it displays a back button. This is useful when the detail entry usually displays a back button but should not display it when being displayed in a `ListDetailScene`. See https://github.com/android/nav3-recipes/issues/151 for more details on this use case.
12+
1113
See `ListDetailScene.kt` for more implementation details.

0 commit comments

Comments
 (0)