Add routes to Fragment and Activity kotlin dsl
Now that routes are supported in Navigation, the Fragment and Activity
destination builders should be updated to take routes as well.
RelNote: "Both the Fragment and Activity destinations builders now
support using routes."
Test: updated all builder tests
Bug: 188914648
Change-Id: Ib5544bd128a17f0dc681e105a1ce4a8a28945bc3
diff --git a/navigation/navigation-fragment/api/current.txt b/navigation/navigation-fragment/api/current.txt
index faa9b59..ce7ec82 100644
--- a/navigation/navigation-fragment/api/current.txt
+++ b/navigation/navigation-fragment/api/current.txt
@@ -68,12 +68,15 @@
@androidx.navigation.NavDestinationDsl public final class FragmentNavigatorDestinationBuilder extends androidx.navigation.NavDestinationBuilder<androidx.navigation.fragment.FragmentNavigator.Destination> {
ctor public FragmentNavigatorDestinationBuilder(androidx.navigation.fragment.FragmentNavigator navigator, @IdRes int id, kotlin.reflect.KClass<? extends androidx.fragment.app.Fragment> fragmentClass);
+ ctor public FragmentNavigatorDestinationBuilder(androidx.navigation.fragment.FragmentNavigator navigator, String route, kotlin.reflect.KClass<? extends androidx.fragment.app.Fragment> fragmentClass);
method public androidx.navigation.fragment.FragmentNavigator.Destination build();
}
public final class FragmentNavigatorDestinationBuilderKt {
method public static inline <reified F extends androidx.fragment.app.Fragment> void fragment(androidx.navigation.NavGraphBuilder, @IdRes int id);
method public static inline <reified F extends androidx.fragment.app.Fragment> void fragment(androidx.navigation.NavGraphBuilder, @IdRes int id, kotlin.jvm.functions.Function1<? super androidx.navigation.fragment.FragmentNavigatorDestinationBuilder,? extends kotlin.Unit> builder);
+ method public static inline <reified F extends androidx.fragment.app.Fragment> void fragment(androidx.navigation.NavGraphBuilder, String route);
+ method public static inline <reified F extends androidx.fragment.app.Fragment> void fragment(androidx.navigation.NavGraphBuilder, String route, kotlin.jvm.functions.Function1<? super androidx.navigation.fragment.FragmentNavigatorDestinationBuilder,? extends kotlin.Unit> builder);
}
public final class FragmentNavigatorExtrasKt {
diff --git a/navigation/navigation-fragment/api/public_plus_experimental_current.txt b/navigation/navigation-fragment/api/public_plus_experimental_current.txt
index faa9b59..ce7ec82 100644
--- a/navigation/navigation-fragment/api/public_plus_experimental_current.txt
+++ b/navigation/navigation-fragment/api/public_plus_experimental_current.txt
@@ -68,12 +68,15 @@
@androidx.navigation.NavDestinationDsl public final class FragmentNavigatorDestinationBuilder extends androidx.navigation.NavDestinationBuilder<androidx.navigation.fragment.FragmentNavigator.Destination> {
ctor public FragmentNavigatorDestinationBuilder(androidx.navigation.fragment.FragmentNavigator navigator, @IdRes int id, kotlin.reflect.KClass<? extends androidx.fragment.app.Fragment> fragmentClass);
+ ctor public FragmentNavigatorDestinationBuilder(androidx.navigation.fragment.FragmentNavigator navigator, String route, kotlin.reflect.KClass<? extends androidx.fragment.app.Fragment> fragmentClass);
method public androidx.navigation.fragment.FragmentNavigator.Destination build();
}
public final class FragmentNavigatorDestinationBuilderKt {
method public static inline <reified F extends androidx.fragment.app.Fragment> void fragment(androidx.navigation.NavGraphBuilder, @IdRes int id);
method public static inline <reified F extends androidx.fragment.app.Fragment> void fragment(androidx.navigation.NavGraphBuilder, @IdRes int id, kotlin.jvm.functions.Function1<? super androidx.navigation.fragment.FragmentNavigatorDestinationBuilder,? extends kotlin.Unit> builder);
+ method public static inline <reified F extends androidx.fragment.app.Fragment> void fragment(androidx.navigation.NavGraphBuilder, String route);
+ method public static inline <reified F extends androidx.fragment.app.Fragment> void fragment(androidx.navigation.NavGraphBuilder, String route, kotlin.jvm.functions.Function1<? super androidx.navigation.fragment.FragmentNavigatorDestinationBuilder,? extends kotlin.Unit> builder);
}
public final class FragmentNavigatorExtrasKt {
diff --git a/navigation/navigation-fragment/api/restricted_current.txt b/navigation/navigation-fragment/api/restricted_current.txt
index faa9b59..ce7ec82 100644
--- a/navigation/navigation-fragment/api/restricted_current.txt
+++ b/navigation/navigation-fragment/api/restricted_current.txt
@@ -68,12 +68,15 @@
@androidx.navigation.NavDestinationDsl public final class FragmentNavigatorDestinationBuilder extends androidx.navigation.NavDestinationBuilder<androidx.navigation.fragment.FragmentNavigator.Destination> {
ctor public FragmentNavigatorDestinationBuilder(androidx.navigation.fragment.FragmentNavigator navigator, @IdRes int id, kotlin.reflect.KClass<? extends androidx.fragment.app.Fragment> fragmentClass);
+ ctor public FragmentNavigatorDestinationBuilder(androidx.navigation.fragment.FragmentNavigator navigator, String route, kotlin.reflect.KClass<? extends androidx.fragment.app.Fragment> fragmentClass);
method public androidx.navigation.fragment.FragmentNavigator.Destination build();
}
public final class FragmentNavigatorDestinationBuilderKt {
method public static inline <reified F extends androidx.fragment.app.Fragment> void fragment(androidx.navigation.NavGraphBuilder, @IdRes int id);
method public static inline <reified F extends androidx.fragment.app.Fragment> void fragment(androidx.navigation.NavGraphBuilder, @IdRes int id, kotlin.jvm.functions.Function1<? super androidx.navigation.fragment.FragmentNavigatorDestinationBuilder,? extends kotlin.Unit> builder);
+ method public static inline <reified F extends androidx.fragment.app.Fragment> void fragment(androidx.navigation.NavGraphBuilder, String route);
+ method public static inline <reified F extends androidx.fragment.app.Fragment> void fragment(androidx.navigation.NavGraphBuilder, String route, kotlin.jvm.functions.Function1<? super androidx.navigation.fragment.FragmentNavigatorDestinationBuilder,? extends kotlin.Unit> builder);
}
public final class FragmentNavigatorExtrasKt {
diff --git a/navigation/navigation-fragment/src/androidTest/java/androidx/navigation/fragment/FragmentNavigatorDestinationBuilderTest.kt b/navigation/navigation-fragment/src/androidTest/java/androidx/navigation/fragment/FragmentNavigatorDestinationBuilderTest.kt
index 028a90c..6e28a13 100644
--- a/navigation/navigation-fragment/src/androidTest/java/androidx/navigation/fragment/FragmentNavigatorDestinationBuilderTest.kt
+++ b/navigation/navigation-fragment/src/androidTest/java/androidx/navigation/fragment/FragmentNavigatorDestinationBuilderTest.kt
@@ -82,8 +82,55 @@
LABEL, graph[DESTINATION_ID].label
)
}
+
+ @UiThreadTest
+ @Test fun fragmentRoute() {
+ val navHostFragment = NavHostFragment()
+ fragmentManager.beginTransaction()
+ .add(android.R.id.content, navHostFragment)
+ .commitNow()
+ val graph = navHostFragment.createGraph(startDestination = DESTINATION_ROUTE) {
+ fragment<BuilderTestFragment>(DESTINATION_ROUTE)
+ }
+ assertTrue(
+ "Destination should be added to the graph",
+ DESTINATION_ROUTE in graph
+ )
+ assertEquals(
+ "Fragment class should be set to BuilderTestFragment",
+ BuilderTestFragment::class.java.name,
+ (graph[DESTINATION_ROUTE] as FragmentNavigator.Destination).className
+ )
+ }
+
+ @UiThreadTest
+ @Test fun fragmentWithBodyRoute() {
+ val navHostFragment = NavHostFragment()
+ fragmentManager.beginTransaction()
+ .add(android.R.id.content, navHostFragment)
+ .commitNow()
+ val graph = navHostFragment.createGraph(startDestination = DESTINATION_ROUTE) {
+ fragment<BuilderTestFragment>(DESTINATION_ROUTE) {
+ label = LABEL
+ }
+ }
+ assertTrue(
+ "Destination should be added to the graph",
+ DESTINATION_ROUTE in graph
+ )
+ assertEquals(
+ "Fragment class should be set to BuilderTestFragment",
+ BuilderTestFragment::class.java.name,
+ (graph[DESTINATION_ROUTE] as FragmentNavigator.Destination).className
+ )
+ assertEquals(
+ "Fragment should have label set",
+ LABEL, graph[DESTINATION_ROUTE].label
+ )
+ }
}
private const val DESTINATION_ID = 1
+private const val DESTINATION_ROUTE = "destination"
private const val LABEL = "Test"
class BuilderTestFragment : Fragment()
diff --git a/navigation/navigation-fragment/src/main/java/androidx/navigation/fragment/FragmentNavigatorDestinationBuilder.kt b/navigation/navigation-fragment/src/main/java/androidx/navigation/fragment/FragmentNavigatorDestinationBuilder.kt
index 2a85478c..60eda99 100644
--- a/navigation/navigation-fragment/src/main/java/androidx/navigation/fragment/FragmentNavigatorDestinationBuilder.kt
+++ b/navigation/navigation-fragment/src/main/java/androidx/navigation/fragment/FragmentNavigatorDestinationBuilder.kt
@@ -46,14 +46,50 @@
)
/**
+ * Construct a new [FragmentNavigator.Destination]
+ */
+public inline fun <reified F : Fragment> NavGraphBuilder.fragment(
+ route: String
+): Unit = fragment<F>(route) {}
+
+/**
+ * Construct a new [FragmentNavigator.Destination]
+ */
+public inline fun <reified F : Fragment> NavGraphBuilder.fragment(
+ route: String,
+ builder: FragmentNavigatorDestinationBuilder.() -> Unit
+): Unit = destination(
+ FragmentNavigatorDestinationBuilder(
+ provider[FragmentNavigator::class],
+ route,
+ F::class
+ ).apply(builder)
+)
+
+/**
* DSL for constructing a new [FragmentNavigator.Destination]
*/
@NavDestinationDsl
-public class FragmentNavigatorDestinationBuilder(
- navigator: FragmentNavigator,
- @IdRes id: Int,
- private val fragmentClass: KClass<out Fragment>
-) : NavDestinationBuilder<FragmentNavigator.Destination>(navigator, id) {
+public class FragmentNavigatorDestinationBuilder :
+ NavDestinationBuilder<FragmentNavigator.Destination> {
+
+ private var fragmentClass: KClass<out Fragment>
+
+ public constructor(
+ navigator: FragmentNavigator,
+ @IdRes id: Int,
+ fragmentClass: KClass<out Fragment>
+ ) : super(navigator, id) {
+ this.fragmentClass = fragmentClass
+ }
+
+ public constructor(
+ navigator: FragmentNavigator,
+ route: String,
+ fragmentClass: KClass<out Fragment>
+ ) : super(navigator, route) {
+ this.fragmentClass = fragmentClass
+ }
override fun build(): FragmentNavigator.Destination =
super.build().also { destination ->
diff --git a/navigation/navigation-runtime/api/current.txt b/navigation/navigation-runtime/api/current.txt
index 219326d..03eef05 100644
--- a/navigation/navigation-runtime/api/current.txt
+++ b/navigation/navigation-runtime/api/current.txt
@@ -60,6 +60,7 @@
@androidx.navigation.NavDestinationDsl public final class ActivityNavigatorDestinationBuilder extends androidx.navigation.NavDestinationBuilder<androidx.navigation.ActivityNavigator.Destination> {
ctor public ActivityNavigatorDestinationBuilder(androidx.navigation.ActivityNavigator navigator, @IdRes int id);
+ ctor public ActivityNavigatorDestinationBuilder(androidx.navigation.ActivityNavigator navigator, String route);
method public androidx.navigation.ActivityNavigator.Destination build();
method public String? getAction();
method public kotlin.reflect.KClass<? extends android.app.Activity>? getActivityClass();
@@ -80,6 +81,7 @@
public final class ActivityNavigatorDestinationBuilderKt {
method public static inline void activity(androidx.navigation.NavGraphBuilder, @IdRes int id, kotlin.jvm.functions.Function1<? super androidx.navigation.ActivityNavigatorDestinationBuilder,kotlin.Unit> builder);
+ method public static inline void activity(androidx.navigation.NavGraphBuilder, String route, kotlin.jvm.functions.Function1<? super androidx.navigation.ActivityNavigatorDestinationBuilder,kotlin.Unit> builder);
}
public final class ActivityNavigatorExtrasKt {
@@ -151,7 +153,7 @@
public final class NavControllerKt {
method public static inline androidx.navigation.NavGraph createGraph(androidx.navigation.NavController, optional @IdRes int id, @IdRes int startDestination, kotlin.jvm.functions.Function1<? super androidx.navigation.NavGraphBuilder,kotlin.Unit> builder);
- method public static androidx.navigation.NavGraph createGraph(androidx.navigation.NavController, String startDestination, optional String? route, kotlin.jvm.functions.Function1<? super androidx.navigation.NavGraphBuilder,kotlin.Unit> builder);
+ method public static inline androidx.navigation.NavGraph createGraph(androidx.navigation.NavController, String startDestination, optional String? route, kotlin.jvm.functions.Function1<? super androidx.navigation.NavGraphBuilder,kotlin.Unit> builder);
}
public final class NavDeepLinkBuilder {
@@ -188,6 +190,7 @@
public final class NavHostKt {
method public static inline androidx.navigation.NavGraph createGraph(androidx.navigation.NavHost, optional @IdRes int id, @IdRes int startDestination, kotlin.jvm.functions.Function1<? super androidx.navigation.NavGraphBuilder,kotlin.Unit> builder);
+ method public static inline androidx.navigation.NavGraph createGraph(androidx.navigation.NavHost, String startDestination, optional String? route, kotlin.jvm.functions.Function1<? super androidx.navigation.NavGraphBuilder,kotlin.Unit> builder);
}
public final class NavInflater {
diff --git a/navigation/navigation-runtime/api/public_plus_experimental_current.txt b/navigation/navigation-runtime/api/public_plus_experimental_current.txt
index 308daa9..f98c73a 100644
--- a/navigation/navigation-runtime/api/public_plus_experimental_current.txt
+++ b/navigation/navigation-runtime/api/public_plus_experimental_current.txt
@@ -62,6 +62,7 @@
@androidx.navigation.NavDestinationDsl public final class ActivityNavigatorDestinationBuilder extends androidx.navigation.NavDestinationBuilder<androidx.navigation.ActivityNavigator.Destination> {
ctor public ActivityNavigatorDestinationBuilder(androidx.navigation.ActivityNavigator navigator, @IdRes int id);
+ ctor public ActivityNavigatorDestinationBuilder(androidx.navigation.ActivityNavigator navigator, String route);
method public androidx.navigation.ActivityNavigator.Destination build();
method public String? getAction();
method public kotlin.reflect.KClass<? extends android.app.Activity>? getActivityClass();
@@ -82,6 +83,7 @@
public final class ActivityNavigatorDestinationBuilderKt {
method public static inline void activity(androidx.navigation.NavGraphBuilder, @IdRes int id, kotlin.jvm.functions.Function1<? super androidx.navigation.ActivityNavigatorDestinationBuilder,kotlin.Unit> builder);
+ method public static inline void activity(androidx.navigation.NavGraphBuilder, String route, kotlin.jvm.functions.Function1<? super androidx.navigation.ActivityNavigatorDestinationBuilder,kotlin.Unit> builder);
}
public final class ActivityNavigatorExtrasKt {
@@ -165,7 +167,7 @@
public final class NavControllerKt {
method public static inline androidx.navigation.NavGraph createGraph(androidx.navigation.NavController, optional @IdRes int id, @IdRes int startDestination, kotlin.jvm.functions.Function1<? super androidx.navigation.NavGraphBuilder,kotlin.Unit> builder);
- method public static androidx.navigation.NavGraph createGraph(androidx.navigation.NavController, String startDestination, optional String? route, kotlin.jvm.functions.Function1<? super androidx.navigation.NavGraphBuilder,kotlin.Unit> builder);
+ method public static inline androidx.navigation.NavGraph createGraph(androidx.navigation.NavController, String startDestination, optional String? route, kotlin.jvm.functions.Function1<? super androidx.navigation.NavGraphBuilder,kotlin.Unit> builder);
}
public final class NavDeepLinkBuilder {
@@ -202,6 +204,7 @@
public final class NavHostKt {
method public static inline androidx.navigation.NavGraph createGraph(androidx.navigation.NavHost, optional @IdRes int id, @IdRes int startDestination, kotlin.jvm.functions.Function1<? super androidx.navigation.NavGraphBuilder,kotlin.Unit> builder);
+ method public static inline androidx.navigation.NavGraph createGraph(androidx.navigation.NavHost, String startDestination, optional String? route, kotlin.jvm.functions.Function1<? super androidx.navigation.NavGraphBuilder,kotlin.Unit> builder);
}
public final class NavInflater {
diff --git a/navigation/navigation-runtime/api/restricted_current.txt b/navigation/navigation-runtime/api/restricted_current.txt
index 219326d..03eef05 100644
--- a/navigation/navigation-runtime/api/restricted_current.txt
+++ b/navigation/navigation-runtime/api/restricted_current.txt
@@ -60,6 +60,7 @@
@androidx.navigation.NavDestinationDsl public final class ActivityNavigatorDestinationBuilder extends androidx.navigation.NavDestinationBuilder<androidx.navigation.ActivityNavigator.Destination> {
ctor public ActivityNavigatorDestinationBuilder(androidx.navigation.ActivityNavigator navigator, @IdRes int id);
+ ctor public ActivityNavigatorDestinationBuilder(androidx.navigation.ActivityNavigator navigator, String route);
method public androidx.navigation.ActivityNavigator.Destination build();
method public String? getAction();
method public kotlin.reflect.KClass<? extends android.app.Activity>? getActivityClass();
@@ -80,6 +81,7 @@
public final class ActivityNavigatorDestinationBuilderKt {
method public static inline void activity(androidx.navigation.NavGraphBuilder, @IdRes int id, kotlin.jvm.functions.Function1<? super androidx.navigation.ActivityNavigatorDestinationBuilder,kotlin.Unit> builder);
+ method public static inline void activity(androidx.navigation.NavGraphBuilder, String route, kotlin.jvm.functions.Function1<? super androidx.navigation.ActivityNavigatorDestinationBuilder,kotlin.Unit> builder);
}
public final class ActivityNavigatorExtrasKt {
@@ -151,7 +153,7 @@
public final class NavControllerKt {
method public static inline androidx.navigation.NavGraph createGraph(androidx.navigation.NavController, optional @IdRes int id, @IdRes int startDestination, kotlin.jvm.functions.Function1<? super androidx.navigation.NavGraphBuilder,kotlin.Unit> builder);
- method public static androidx.navigation.NavGraph createGraph(androidx.navigation.NavController, String startDestination, optional String? route, kotlin.jvm.functions.Function1<? super androidx.navigation.NavGraphBuilder,kotlin.Unit> builder);
+ method public static inline androidx.navigation.NavGraph createGraph(androidx.navigation.NavController, String startDestination, optional String? route, kotlin.jvm.functions.Function1<? super androidx.navigation.NavGraphBuilder,kotlin.Unit> builder);
}
public final class NavDeepLinkBuilder {
@@ -188,6 +190,7 @@
public final class NavHostKt {
method public static inline androidx.navigation.NavGraph createGraph(androidx.navigation.NavHost, optional @IdRes int id, @IdRes int startDestination, kotlin.jvm.functions.Function1<? super androidx.navigation.NavGraphBuilder,kotlin.Unit> builder);
+ method public static inline androidx.navigation.NavGraph createGraph(androidx.navigation.NavHost, String startDestination, optional String? route, kotlin.jvm.functions.Function1<? super androidx.navigation.NavGraphBuilder,kotlin.Unit> builder);
}
public final class NavInflater {
diff --git a/navigation/navigation-runtime/src/androidTest/java/androidx/navigation/ActivityNavigatorDestinationBuilderTest.kt b/navigation/navigation-runtime/src/androidTest/java/androidx/navigation/ActivityNavigatorDestinationBuilderTest.kt
index 41b28c1..53c3aea 100644
--- a/navigation/navigation-runtime/src/androidTest/java/androidx/navigation/ActivityNavigatorDestinationBuilderTest.kt
+++ b/navigation/navigation-runtime/src/androidTest/java/androidx/navigation/ActivityNavigatorDestinationBuilderTest.kt
@@ -136,9 +136,115 @@
(graph[DESTINATION_ID] as ActivityNavigator.Destination).dataPattern
)
}
+
+ @Test
+ fun activityRoute() {
+ val graph = navController.createGraph(startDestination = DESTINATION_ROUTE) {
+ activity(DESTINATION_ROUTE) {
+ label = LABEL
+ }
+ }
+ assertTrue(
+ "Destination should be added to the graph",
+ DESTINATION_ROUTE in graph
+ )
+ assertEquals(
+ "Destination should have label set",
+ LABEL,
+ graph[DESTINATION_ROUTE].label
+ )
+ }
+
+ @Test
+ fun activityPackageRoute() {
+ val graph = navController.createGraph(startDestination = DESTINATION_ROUTE) {
+ activity(DESTINATION_ROUTE) {
+ targetPackage = PACKAGE_NAME
+ }
+ }
+ assertWithMessage("Destination should be added to the graph")
+ .that(DESTINATION_ROUTE in graph)
+ .isTrue()
+ assertWithMessage("Destination should have package name set")
+ .that((graph[DESTINATION_ROUTE] as ActivityNavigator.Destination).targetPackage)
+ .isEqualTo(PACKAGE_NAME)
+ }
+
+ @Test
+ fun activityClassRoute() {
+ val graph = navController.createGraph(startDestination = DESTINATION_ROUTE) {
+ activity(DESTINATION_ROUTE) {
+ activityClass = TestActivity::class
+ }
+ }
+ assertTrue(
+ "Destination should be added to the graph",
+ DESTINATION_ROUTE in graph
+ )
+ assertEquals(
+ "Destination should have ComponentName set",
+ TestActivity::class.java.name,
+ (graph[DESTINATION_ROUTE] as ActivityNavigator.Destination).component?.className
+ )
+ }
+
+ @Test
+ fun actionRoute() {
+ val graph = navController.createGraph(startDestination = DESTINATION_ROUTE) {
+ activity(DESTINATION_ROUTE) {
+ action = ACTION
+ }
+ }
+ assertTrue(
+ "Destination should be added to the graph",
+ DESTINATION_ROUTE in graph
+ )
+ assertEquals(
+ "Destination should have action set",
+ ACTION,
+ (graph[DESTINATION_ROUTE] as ActivityNavigator.Destination).action
+ )
+ }
+
+ @Test
+ fun dataRoute() {
+ val graph = navController.createGraph(startDestination = DESTINATION_ROUTE) {
+ activity(DESTINATION_ROUTE) {
+ data = DATA
+ }
+ }
+ assertTrue(
+ "Destination should be added to the graph",
+ DESTINATION_ROUTE in graph
+ )
+ assertEquals(
+ "Destination should have data set",
+ DATA,
+ (graph[DESTINATION_ROUTE] as ActivityNavigator.Destination).data
+ )
+ }
+
+ @Test
+ fun dataPatternRoute() {
+ val graph = navController.createGraph(startDestination = DESTINATION_ROUTE) {
+ activity(DESTINATION_ROUTE) {
+ dataPattern = DATA_PATTERN
+ }
+ }
+ assertTrue(
+ "Destination should be added to the graph",
+ DESTINATION_ROUTE in graph
+ )
+ assertEquals(
+ "Destination should have data pattern set",
+ DATA_PATTERN,
+ (graph[DESTINATION_ROUTE] as ActivityNavigator.Destination).dataPattern
+ )
+ }
}
private const val DESTINATION_ID = 1
+private const val DESTINATION_ROUTE = "destination"
private const val PACKAGE_NAME = "com.example"
private const val LABEL = "Test"
private const val ACTION = "ACTION_TEST"
diff --git a/navigation/navigation-runtime/src/main/java/androidx/navigation/ActivityNavigatorDestinationBuilder.kt b/navigation/navigation-runtime/src/main/java/androidx/navigation/ActivityNavigatorDestinationBuilder.kt
index 993f80b..1089b72 100644
--- a/navigation/navigation-runtime/src/main/java/androidx/navigation/ActivityNavigatorDestinationBuilder.kt
+++ b/navigation/navigation-runtime/src/main/java/androidx/navigation/ActivityNavigatorDestinationBuilder.kt
@@ -20,6 +20,7 @@
import android.app.Activity
import android.content.ComponentName
+import android.content.Context
import android.net.Uri
import androidx.annotation.IdRes
import kotlin.reflect.KClass
@@ -38,14 +39,33 @@
)
/**
+ * Construct a new [ActivityNavigator.Destination]
+ */
+public inline fun NavGraphBuilder.activity(
+ route: String,
+ builder: ActivityNavigatorDestinationBuilder.() -> Unit
+): Unit = destination(
+ ActivityNavigatorDestinationBuilder(
+ provider[ActivityNavigator::class],
+ route
+ ).apply(builder)
+)
+
+/**
* DSL for constructing a new [ActivityNavigator.Destination]
*/
@NavDestinationDsl
-public class ActivityNavigatorDestinationBuilder(
- navigator: ActivityNavigator,
- @IdRes id: Int
-) : NavDestinationBuilder<ActivityNavigator.Destination>(navigator, id) {
- private val context = navigator.context
+public class ActivityNavigatorDestinationBuilder :
+ NavDestinationBuilder<ActivityNavigator.Destination> {
+ private var context: Context
+
+ public constructor(navigator: ActivityNavigator, @IdRes id: Int) : super(navigator, id) {
+ context = navigator.context
+ }
+
+ public constructor(navigator: ActivityNavigator, route: String) : super(navigator, route) {
+ context = navigator.context
+ }
public var targetPackage: String? = null
diff --git a/navigation/navigation-runtime/src/main/java/androidx/navigation/NavController.kt b/navigation/navigation-runtime/src/main/java/androidx/navigation/NavController.kt
index 009d740..7fe7375 100644
--- a/navigation/navigation-runtime/src/main/java/androidx/navigation/NavController.kt
+++ b/navigation/navigation-runtime/src/main/java/androidx/navigation/NavController.kt
@@ -1962,7 +1962,7 @@
* @param route the route for the graph
* @param builder the builder used to construct the graph
*/
-public fun NavController.createGraph(
+public inline fun NavController.createGraph(
startDestination: String,
route: String? = null,
builder: NavGraphBuilder.() -> Unit
diff --git a/navigation/navigation-runtime/src/main/java/androidx/navigation/NavHost.kt b/navigation/navigation-runtime/src/main/java/androidx/navigation/NavHost.kt
index 979dec7..bb6b996 100644
--- a/navigation/navigation-runtime/src/main/java/androidx/navigation/NavHost.kt
+++ b/navigation/navigation-runtime/src/main/java/androidx/navigation/NavHost.kt
@@ -58,3 +58,12 @@
@IdRes startDestination: Int,
builder: NavGraphBuilder.() -> Unit
): NavGraph = navController.createGraph(id, startDestination, builder)
+
+/**
+ * Construct a new [NavGraph]
+ */
+public inline fun NavHost.createGraph(
+ startDestination: String,
+ route: String? = null,
+ builder: NavGraphBuilder.() -> Unit
+): NavGraph = navController.createGraph(startDestination, route, builder)