Skip to content

Commit a8ec186

Browse files
Added Code Snippets for Decorators (#690)
* Added Code Snippets for Decorators
1 parent fe3bf10 commit a8ec186

File tree

1 file changed

+64
-0
lines changed
  • compose/snippets/src/main/java/com/example/compose/snippets/navigation3/decorators

1 file changed

+64
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.compose.snippets.navigation3.decorators
18+
19+
import android.util.Log
20+
import androidx.compose.material3.Text
21+
import androidx.compose.runtime.Composable
22+
import androidx.compose.runtime.remember
23+
import androidx.navigation3.runtime.NavEntryDecorator
24+
import androidx.navigation3.runtime.NavKey
25+
import androidx.navigation3.runtime.entryProvider
26+
import androidx.navigation3.runtime.rememberNavBackStack
27+
import androidx.navigation3.runtime.rememberSaveableStateHolderNavEntryDecorator
28+
import androidx.navigation3.ui.NavDisplay
29+
import com.example.compose.snippets.navigation3.savingstate.Home
30+
import kotlinx.serialization.Serializable
31+
32+
// [START android_compose_navigation3_decorator_1]
33+
// import androidx.navigation3.runtime.NavEntryDecorator
34+
class CustomNavEntryDecorator<T : Any> : NavEntryDecorator<T>(
35+
decorate = { entry ->
36+
Log.d("CustomNavEntryDecorator", "entry with ${entry.contentKey} entered composition and was decorated")
37+
entry.Content()
38+
},
39+
onPop = { contentKey -> Log.d("CustomNavEntryDecorator", "entry with $contentKey was popped") }
40+
)
41+
// [END android_compose_navigation3_decorator_1]
42+
43+
@Serializable
44+
data object Home : NavKey
45+
46+
@Composable
47+
fun DecoratorsBasic() {
48+
// [START android_compose_navigation3_decorator_2]
49+
50+
// import androidx.navigation3.runtime.rememberSaveableStateHolderNavEntryDecorator
51+
NavDisplay(
52+
entryDecorators = listOf(
53+
rememberSaveableStateHolderNavEntryDecorator(),
54+
remember { CustomNavEntryDecorator() }
55+
),
56+
// [START_EXCLUDE]
57+
backStack = rememberNavBackStack(Home),
58+
entryProvider = entryProvider {
59+
entry<Home> { Text("Welcome to Nav3") }
60+
}
61+
// [END_EXCLUDE]
62+
)
63+
// [END android_compose_navigation3_decorator_2]
64+
}

0 commit comments

Comments
 (0)