|
| 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 | + * http://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.nav3recipes.dialog |
| 18 | + |
| 19 | +import android.os.Bundle |
| 20 | +import androidx.activity.ComponentActivity |
| 21 | +import androidx.activity.compose.setContent |
| 22 | +import androidx.compose.foundation.shape.RoundedCornerShape |
| 23 | +import androidx.compose.material3.Button |
| 24 | +import androidx.compose.material3.Text |
| 25 | +import androidx.compose.runtime.remember |
| 26 | +import androidx.compose.ui.Modifier |
| 27 | +import androidx.compose.ui.draw.clip |
| 28 | +import androidx.compose.ui.unit.dp |
| 29 | +import androidx.compose.ui.window.DialogProperties |
| 30 | +import androidx.navigation3.runtime.NavKey |
| 31 | +import androidx.navigation3.runtime.entry |
| 32 | +import androidx.navigation3.runtime.entryProvider |
| 33 | +import androidx.navigation3.runtime.rememberNavBackStack |
| 34 | +import androidx.navigation3.ui.DialogSceneStrategy |
| 35 | +import androidx.navigation3.ui.NavDisplay |
| 36 | +import com.example.nav3recipes.content.ContentBlue |
| 37 | +import com.example.nav3recipes.content.ContentGreen |
| 38 | +import com.example.nav3recipes.ui.setEdgeToEdgeConfig |
| 39 | +import kotlinx.serialization.Serializable |
| 40 | + |
| 41 | +/** |
| 42 | + * This recipe demonstrates how to create a dialog. It does this by: |
| 43 | + * |
| 44 | + * - Adding the `DialogSceneStrategy` to the list of strategies used by `NavDisplay`. |
| 45 | + * - Adding `DialogSceneStrategy.dialog` to a `NavEntry`'s metadata to indicate that it is a |
| 46 | + * dialog. In this case it is applied to the `NavEntry` for `RouteB`. |
| 47 | + * |
| 48 | + * See also https://developer.android.com/guide/navigation/navigation-3/custom-layouts |
| 49 | + */ |
| 50 | + |
| 51 | +@Serializable |
| 52 | +private data object RouteA : NavKey |
| 53 | + |
| 54 | +@Serializable |
| 55 | +private data class RouteB(val id: String) : NavKey |
| 56 | + |
| 57 | +class DialogActivity : ComponentActivity() { |
| 58 | + |
| 59 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 60 | + setEdgeToEdgeConfig() |
| 61 | + super.onCreate(savedInstanceState) |
| 62 | + setContent { |
| 63 | + val backStack = rememberNavBackStack(RouteA) |
| 64 | + val dialogStrategy = remember { DialogSceneStrategy<NavKey>() } |
| 65 | + |
| 66 | + NavDisplay( |
| 67 | + backStack = backStack, |
| 68 | + onBack = { backStack.removeLastOrNull() }, |
| 69 | + sceneStrategy = dialogStrategy, |
| 70 | + entryProvider = entryProvider { |
| 71 | + entry<RouteA> { |
| 72 | + ContentGreen("Welcome to Nav3") { |
| 73 | + Button(onClick = { |
| 74 | + backStack.add(RouteB("123")) |
| 75 | + }) { |
| 76 | + Text("Click to open dialog") |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + entry<RouteB>( |
| 81 | + metadata = DialogSceneStrategy.dialog( |
| 82 | + DialogProperties(windowTitle = "Route B dialog") |
| 83 | + ) |
| 84 | + ) { key -> |
| 85 | + ContentBlue( |
| 86 | + title = "Route id: ${key.id}", |
| 87 | + modifier = Modifier.clip( |
| 88 | + shape = RoundedCornerShape(16.dp) |
| 89 | + ) |
| 90 | + ) |
| 91 | + } |
| 92 | + } |
| 93 | + ) |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments