Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3e509ff

Browse files
authoredMar 7, 2025
Fix Wear navigation startDestination (android#1538)
Also add a unit test covering activity startup and navigation
2 parents 764a75f + 07f4d2b commit 3e509ff

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed
 

Diff for: ‎Jetcaster/wear/src/main/java/com/example/jetcaster/MainActivity.kt

+7-1
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,22 @@ import android.os.Bundle
2020
import androidx.activity.ComponentActivity
2121
import androidx.activity.compose.setContent
2222
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
23+
import androidx.navigation.NavHostController
24+
import androidx.wear.compose.navigation.rememberSwipeDismissableNavController
2325
import dagger.hilt.android.AndroidEntryPoint
2426

2527
@AndroidEntryPoint
2628
class MainActivity : ComponentActivity() {
29+
lateinit var navController: NavHostController
30+
2731
override fun onCreate(savedInstanceState: Bundle?) {
2832
installSplashScreen()
2933
super.onCreate(savedInstanceState)
3034

3135
setContent {
32-
WearApp()
36+
navController = rememberSwipeDismissableNavController()
37+
38+
WearApp(navController)
3339
}
3440
}
3541
}

Diff for: ‎Jetcaster/wear/src/main/java/com/example/jetcaster/WearApp.kt

+3-5
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ import androidx.compose.ui.Modifier
2525
import androidx.compose.ui.graphics.Color
2626
import androidx.lifecycle.compose.collectAsStateWithLifecycle
2727
import androidx.lifecycle.viewmodel.compose.viewModel
28+
import androidx.navigation.NavHostController
2829
import androidx.wear.compose.navigation.SwipeDismissableNavHost
2930
import androidx.wear.compose.navigation.composable
30-
import androidx.wear.compose.navigation.rememberSwipeDismissableNavController
3131
import androidx.wear.compose.navigation.rememberSwipeDismissableNavHostState
3232
import com.example.jetcaster.theme.WearAppTheme
3333
import com.example.jetcaster.ui.Episode
@@ -57,16 +57,14 @@ import com.google.android.horologist.media.ui.navigation.NavigationScreens
5757
import com.google.android.horologist.media.ui.screens.playerlibrarypager.PlayerLibraryPagerScreen
5858

5959
@Composable
60-
fun WearApp() {
61-
62-
val navController = rememberSwipeDismissableNavController()
60+
fun WearApp(navController: NavHostController) {
6361
val navHostState = rememberSwipeDismissableNavHostState()
6462
val volumeViewModel: VolumeViewModel = viewModel(factory = VolumeViewModel.Factory)
6563

6664
WearAppTheme {
6765
AppScaffold {
6866
SwipeDismissableNavHost(
69-
startDestination = NavigationScreens.Player.navRoute,
67+
startDestination = NavigationScreens.Player.playerDestination(),
7068
navController = navController,
7169
modifier = Modifier.background(Color.Transparent),
7270
state = navHostState,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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.jetcaster
18+
19+
import androidx.compose.ui.test.junit4.createAndroidComposeRule
20+
import com.example.jetcaster.ui.JetcasterNavController.navigateToUpNext
21+
import org.junit.Assert.assertEquals
22+
import org.junit.Rule
23+
import org.junit.Test
24+
import org.junit.runner.RunWith
25+
import org.robolectric.RobolectricTestRunner
26+
import org.robolectric.annotation.Config
27+
import org.robolectric.annotation.GraphicsMode
28+
29+
@RunWith(RobolectricTestRunner::class)
30+
@Config(sdk = [34])
31+
@GraphicsMode(GraphicsMode.Mode.NATIVE)
32+
class NavigationTest {
33+
@get:Rule
34+
val rule = createAndroidComposeRule(MainActivity::class.java)
35+
36+
@Test
37+
fun launchAndNavigate() {
38+
val activity = rule.activity
39+
40+
val navController = activity.navController
41+
42+
rule.waitUntil {
43+
navController.currentDestination?.route != null
44+
}
45+
46+
assertEquals("player?page={page}", navController.currentDestination?.route)
47+
48+
navController.navigateToUpNext()
49+
50+
assertEquals("upNext", navController.currentDestination?.route)
51+
}
52+
}

0 commit comments

Comments
 (0)
Please sign in to comment.