Skip to content

Add Navigation3 snippets #527

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: latest
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions compose/snippets/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ dependencies {
implementation(libs.androidx.compose.material3.adaptive.layout)
implementation(libs.androidx.compose.material3.adaptive.navigation)
implementation(libs.androidx.compose.material3.adaptive.navigation.suite)
implementation(libs.androidx.compose.material3.windowsizeclass)
implementation(libs.androidx.compose.material)

implementation(libs.androidx.compose.runtime)
Expand Down Expand Up @@ -136,7 +137,13 @@ dependencies {
implementation(libs.androidx.navigation.compose)
implementation(libs.hilt.android)
implementation(libs.androidx.hilt.navigation.compose)

implementation(libs.kotlinx.serialization.core)
implementation(libs.kotlinx.serialization.json)
implementation(libs.androidx.compose.material3.adaptive.navigation3)
implementation(libs.androidx.navigation3.runtime)
implementation(libs.androidx.navigation3.ui)
implementation(libs.androidx.lifecycle.viewmodel.navigation3)

implementation(libs.androidx.recyclerview)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
/*
* Copyright 2025 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.compose.snippets.navigation3

import androidx.compose.animation.ExperimentalSharedTransitionApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import com.example.compose.snippets.ui.theme.PastelBlue
import com.example.compose.snippets.ui.theme.PastelGreen
import com.example.compose.snippets.ui.theme.PastelMauve
import com.example.compose.snippets.ui.theme.PastelOrange
import com.example.compose.snippets.ui.theme.PastelPink
import com.example.compose.snippets.ui.theme.PastelPurple
import com.example.compose.snippets.ui.theme.PastelRed
import com.example.compose.snippets.ui.theme.PastelYellow

@OptIn(ExperimentalSharedTransitionApi::class)
@Composable
fun ContentBase(
title: String,
modifier: Modifier = Modifier,
onNext: (() -> Unit)? = null,
content: (@Composable () -> Unit)? = null,
) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = modifier
.fillMaxSize()
.clip(RoundedCornerShape(48.dp))
) {
Title(title)
if (content != null) content()
if (onNext != null) {
Button(
modifier = Modifier.align(Alignment.CenterHorizontally),
onClick = onNext
) {
Text("Next")
}
}
}
}

@Composable
fun ColumnScope.Title(title: String) {
Text(
modifier = Modifier
.padding(24.dp)
.align(Alignment.CenterHorizontally),
fontWeight = FontWeight.Bold,
text = title
)
}

@Composable
fun ContentRed(
title: String,
modifier: Modifier = Modifier,
onNext: (() -> Unit)? = null,
content: (@Composable () -> Unit)? = null,
) = ContentBase(
title = title,
modifier = modifier.background(PastelRed),
onNext = onNext,
content = content
)

@Composable
fun ContentOrange(
title: String,
modifier: Modifier = Modifier,
onNext: (() -> Unit)? = null,
content: (@Composable () -> Unit)? = null,
) = ContentBase(
title = title,
modifier = modifier.background(PastelOrange),
onNext = onNext,
content = content
)

@Composable
fun ContentYellow(
title: String,
modifier: Modifier = Modifier,
onNext: (() -> Unit)? = null,
content: (@Composable () -> Unit)? = null,
) = ContentBase(
title = title,
modifier = modifier.background(PastelYellow),
onNext = onNext,
content = content
)

@Composable
fun ContentGreen(
title: String,
modifier: Modifier = Modifier,
onNext: (() -> Unit)? = null,
content: (@Composable () -> Unit)? = null,
) = ContentBase(
title = title,
modifier = modifier.background(PastelGreen),
onNext = onNext,
content = content
)

@Composable
fun ContentBlue(
title: String,
modifier: Modifier = Modifier,
onNext: (() -> Unit)? = null,
content: (@Composable () -> Unit)? = null,
) = ContentBase(
title = title,
modifier = modifier.background(PastelBlue),
onNext = onNext,
content = content
)

@Composable
fun ContentMauve(
title: String,
modifier: Modifier = Modifier,
onNext: (() -> Unit)? = null,
content: (@Composable () -> Unit)? = null,
) = ContentBase(
title = title,
modifier = modifier.background(PastelMauve),
onNext = onNext,
content = content
)

@Composable
fun ContentPurple(
title: String,
modifier: Modifier = Modifier,
onNext: (() -> Unit)? = null,
content: (@Composable () -> Unit)? = null,
) = ContentBase(
title = title,
modifier = modifier.background(PastelPurple),
onNext = onNext,
content = content
)

@Composable
fun ContentPink(
title: String,
modifier: Modifier = Modifier,
onNext: (() -> Unit)? = null,
content: (@Composable () -> Unit)? = null,
) = ContentBase(
title = title,
modifier = modifier.background(PastelPink),
onNext = onNext,
content = content
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Copyright 2025 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.compose.snippets.navigation3.animations

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.EnterTransition
import androidx.compose.animation.ExitTransition
import androidx.compose.animation.core.tween
import androidx.compose.animation.slideInHorizontally
import androidx.compose.animation.slideInVertically
import androidx.compose.animation.slideOutHorizontally
import androidx.compose.animation.slideOutVertically
import androidx.compose.animation.togetherWith
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.ui.Modifier
import androidx.navigation3.runtime.NavKey
import androidx.navigation3.runtime.entry
import androidx.navigation3.runtime.entryProvider
import androidx.navigation3.runtime.rememberNavBackStack
import androidx.navigation3.ui.NavDisplay
import com.example.compose.snippets.navigation3.ContentGreen
import com.example.compose.snippets.navigation3.ContentMauve
import com.example.compose.snippets.navigation3.ContentOrange
import kotlinx.serialization.Serializable

// [START android_compose_navigation3_animations_1]
@Serializable
data object ScreenA : NavKey

@Serializable
data object ScreenB : NavKey

@Serializable
data object ScreenC : NavKey

class AnimatedNavDisplayActivity : ComponentActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {

Scaffold { paddingValues ->

val backStack = rememberNavBackStack(ScreenA)

NavDisplay(
backStack = backStack,
onBack = { backStack.removeLastOrNull() },
entryProvider = entryProvider {
entry<ScreenA> {
ContentOrange("This is Screen A") {
Button(onClick = { backStack.add(ScreenB) }) {
Text("Go to Screen B")
}
}
}
entry<ScreenB> {
ContentMauve("This is Screen B") {
Button(onClick = { backStack.add(ScreenC) }) {
Text("Go to Screen C")
}
}
}
entry<ScreenC>(
metadata = NavDisplay.transitionSpec {
// Slide new content up, keeping the old content in place underneath
slideInVertically(
initialOffsetY = { it },
animationSpec = tween(1000)
) togetherWith ExitTransition.KeepUntilTransitionsFinished
} + NavDisplay.popTransitionSpec {
// Slide old content down, revealing the new content in place underneath
EnterTransition.None togetherWith
slideOutVertically(
targetOffsetY = { it },
animationSpec = tween(1000)
)
} + NavDisplay.predictivePopTransitionSpec {
// Slide old content down, revealing the new content in place underneath
EnterTransition.None togetherWith
slideOutVertically(
targetOffsetY = { it },
animationSpec = tween(1000)
)
}
) {
ContentGreen("This is Screen C")
}
},
transitionSpec = {
// Slide in from right when navigating forward
slideInHorizontally(initialOffsetX = { it }) togetherWith
slideOutHorizontally(targetOffsetX = { -it })
},
popTransitionSpec = {
// Slide in from left when navigating back
slideInHorizontally(initialOffsetX = { -it }) togetherWith
slideOutHorizontally(targetOffsetX = { it })
},
predictivePopTransitionSpec = {
// Slide in from left when navigating back
slideInHorizontally(initialOffsetX = { -it }) togetherWith
slideOutHorizontally(targetOffsetX = { it })
},
modifier = Modifier.padding(paddingValues)
)
}
}
}
}
// [END android_compose_navigation3_animations_1]
Loading
Loading