标签: android-navigation

如何使用 Jetpack Navigation 从嵌套 Fragment 导航到父 Fragment?

我有主导航:SplashFragment -> RegistrationFragment -> RootFragment

<fragment
    android:id="@+id/splashFragment"
    android:name="com.low6.low6.features.splash.SplashFragment"
    android:label="Splash"
    tools:layout="@layout/fragment_splash" >
    <action
        android:id="@+id/action_next"
        app:clearTask="true"
        app:destination="@id/registrationFragment" />
</fragment>

<fragment
    android:id="@+id/registrationFragment"
    android:name="com.low6.low6.features.registration.RegistrationFragment"
    android:label="Register">
    <action
        android:id="@+id/action_next"
        app:clearTask="true"
        app:destination="@id/rootFragment" />
</fragment>

<fragment
    android:id="@+id/rootFragment"
    android:name="com.low6.low6.core.RootFragment"
    android:label="@string/home"
    tools:layout="@layout/fragment_root" />
Run Code Online (Sandbox Code Playgroud)

我有嵌套的注册导航: RegistrationPersonalFragment -> RegistrationContactFragment -> RegistrationSecurityFragment

<fragment
    android:id="@+id/registrationPersonalFragment"
    android:name="com.low6.low6.features.registration.RegistrationPersonalFragment"
    android:label="Register">

    <action
        android:id="@+id/action_next"
        app:destination="@+id/registrationContactFragment" />
</fragment>

<fragment
    android:id="@+id/registrationContactFragment"
    android:name="com.low6.low6.features.registration.RegistrationContactFragment"
    android:label="Register">

    <action
        android:id="@+id/action_next"
        app:destination="@+id/registrationSecurityFragment" />
</fragment>

<fragment
    android:id="@+id/registrationSecurityFragment"
    android:name="com.low6.low6.features.registration.RegistrationSecurityFragment"
    android:label="Register">

    <action
        android:id="@+id/action_next"
        app:destination="@+id/rootFragment" />
</fragment>
Run Code Online (Sandbox Code Playgroud)

如何使用 Jetpack Navigation 组件从最后一个嵌套的 RegistrationSecurityFragment 重定向到 RootFragment?

目前

<action
    android:id="@+id/action_next"
    app:destination="@+id/rootFragment" />
Run Code Online (Sandbox Code Playgroud)

   navigateTo(R.id.action_next) …
Run Code Online (Sandbox Code Playgroud)

android android-navigation android-jetpack android-architecture-navigation

10
推荐指数
2
解决办法
9939
查看次数

如何在构建时使用占位符和导航组件创建深度链接

我们有一个使用 Deeplinks 的应用程序。我们还使用了 Android 导航组件。

目前我们在导航.xml 文件中配置我们的深层链接,这工作正常,但我们现在需要能够在构建时根据设置的环境变量添加另一个深层链接。

  • 我尝试在 build.gradle 中设置 String 资源并在 navigation.xml 中引用这些资源。

  • 我还尝试在 navigation.xml 中设置占位符,但无法替换它,因为它已被解析为 URI。

  • 我还尝试在 Manifest 中使用占位符设置直接意图过滤器,这会起作用,但我们失去了来自导航组件的良好路由。

目前,我们在 out navigation.xml 文件中以以下形式配置我们的 Deeplinks:

 <deepLink
            android:autoVerify="true"
            app:uri="foo.bar.baz/pull/{quxArg}/{quuxArg}" />
Run Code Online (Sandbox Code Playgroud)

我们现在需要能够在构建时基于一组 Envar 创建额外的 Deeplink。

例子:

DEEPLINK_ENVAR = "replacement.com"

构建.gradle:

manifestPlaceholders = [deeplink:DEEPLINK_ENVAR]
Run Code Online (Sandbox Code Playgroud)

导航.xml:

<deepLink
            android:autoVerify="true"
            app:uri="${deeplink}/pull/{quxArg}/{quuxArg}" />
Run Code Online (Sandbox Code Playgroud)

请注意,以上方法无效。

如果这只是 Manifest 中的一个意图过滤器,我们可以使用 Manifest 占位符来完成此任务并在 app.gradle 中设置它们。然而,navigation.xml 中设置的深层链接被解析为 URI,并在替换之前销毁所有占位符。

有没有人尝试过类似的事情?我试图避免必须运行预构建脚本来直接模板化导航文件。

期望的结果:

我希望能够在构建时添加一个额外的深层链接(实际上 4 个到不同的目的地),同时使用 Android 导航组件。

android placeholder android-navigation android-deep-link

10
推荐指数
1
解决办法
1097
查看次数

如何使用Android导航组件将值传递回之前的片段目的地?

让坐吧 我有一些这样的目的地

从片段 A --> 到片段 B --> 到片段 C

我可以使用 Safe Args 将数据从片段 A 传递到片段 B,也可以使用从片段 B 到片段 C 的安全参数。

如果我想将片段 C 中生成的字符串带回片段 B 或片段 A 怎么办?

要从片段 C 导航到片段 B,我使用代码:

Navigation.findNavController(fragmentView).navigateUp()
Run Code Online (Sandbox Code Playgroud)

并从片段 C 导航到片段 A,我使用代码:

Navigation.findNavController(view).popBackStack(R.id.fragmentA, false)
Run Code Online (Sandbox Code Playgroud)

那么如何将值传递回之前的片段目的地?我是否必须从片段 C 返回到片段 B 并通过安全参数传递值?

真的需要你的帮助。非常感谢 :)

android android-fragments android-navigation android-jetpack android-architecture-navigation

10
推荐指数
2
解决办法
3179
查看次数

Android 导航组件 - 在包含的模块图之间导航

我有一个带有 3 个模块的活动应用程序 - app, list& detail。我的活动在app模块中,它托管唯一的NavHostFragment. 所有模块都有自己的导航图。detail的起点需要一个长参数。app的图是其他图的父图:

<?xml version="1.0" encoding="utf-8"?>
<navigation
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/nav_main"
    app:startDestination="@id/nav_list">

    <include app:graph="@navigation/nav_list" />
    <include app:graph="@navigation/nav_detail" />
</navigation>
Run Code Online (Sandbox Code Playgroud)

但默认情况下,无法向编辑器上的包含图形添加操作:

SS1

我可以在 xml 文件中添加一个全局操作,然后在编辑器中显示:

<?xml version="1.0" encoding="utf-8"?>
<navigation
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/nav_main"
    app:startDestination="@id/nav_list">

    <include app:graph="@navigation/nav_list" />
    <include app:graph="@navigation/nav_detail" />
    
    <action
        android:id="@+id/action_global_detailFragment"
        app:destination="@id/nav_detail" />
</navigation>
Run Code Online (Sandbox Code Playgroud)

我不想使用全局操作,而是添加适当的actions 来封装导航模式。嵌套图已经包含它们的导航逻辑,可能只需要输入作为入口点。我不确定这是否不受支持并且我遗漏了一些东西,否则为什么不呢?在两个或多个包含的图形之间导航的方法是什么?

android android-navigation android-architecture-components android-architecture-navigation

10
推荐指数
1
解决办法
657
查看次数

如何使用嵌套的 navGraph 和底部导航视图进行导航

我有 3 个项目的底部导航视图,我navGraph看起来像这样:

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
app:startDestination="@id/nested_navigation"

<navigation
    android:id="@+id/nested_navigation"
    app:startDestination="@id/mainFragment" >
    <fragment
        android:id="@+id/mainFragment"
        android:name="com.example.app.ui.main.MainFragment"
        android:label="main_fragment"
        tools:layout="@layout/main_fragment" />
    <fragment
        android:id="@+id/list"
        android:name="com.example.app.ui.main.List"
        android:label="fragment_news_list"
        tools:layout="@layout/fragment_list" />
</navigation>

<fragment
    android:id="@+id/settings"
    android:name="com.example.app.ui.main.Settings"
    android:label="Settings" />
</navigation>
Run Code Online (Sandbox Code Playgroud)

带有嵌套 navGraph 片段的底部导航视图中的导航工作正常,但是如果我导航到settings_fragment嵌套 navGraph 之外的 ,并且我单击其他项目/片段我无法导航到其他片段,我基本上卡住在这个屏幕上。

我检查了发生了什么是如果我把settings_fragment嵌套的 navGraph放在里面,它的效果很好。

我该如何解决这个问题?

顺便说一句 - 我很确定它不相关,但设置片段是PreferenceScreen位于 XML 资源中的布局,而不是布局资源

我的菜单项:

<item
    android:id="@+id/mainFragment"
    android:icon="@drawable/ic_home_black_24dp"
    android:title="@string/home"
    app:showAsAction="ifRoom"
    />
<item
    android:id="@+id/list"
    android:icon="@drawable/ic_format_list_bulleted_black_24dp"
    android:title="@string/news_list"
    app:showAsAction="ifRoom"
    />
<item
    android:id="@+id/settings"
    android:icon="@drawable/ic_settings_black_24dp"
    android:title="@string/settings"
    app:showAsAction="ifRoom"
    />
Run Code Online (Sandbox Code Playgroud)

android kotlin android-navigation bottomnavigationview android-jetpack-navigation

10
推荐指数
1
解决办法
1795
查看次数

BottomSheetDialogFragment 中的 NavigationComponent 导航

Android 的导航组件能否用于在 BottomSheet 内导航(即在单个底部工作表中替换/添加片段)?

我知道如何BottomSheetDialogFragment使用<dialog>导航图中的标签启动一个。例如,下面的代码nav_graph.xml允许用户从一个BottomSheetDialogFragment(fragmentOne)导航到另一个BottomSheetDialogFragment(fragmentTwo)。FragmentTwo 作为第二个BottomSheet 打开,位于FragmentOne 的BottomSheet 之上。

但是,如果我想让 fragmentTwo 替换同一个 BottomSheet 中的 fragmentOne 怎么办?我将如何使用导航图完成此操作?

<navigation android:id="@+id/nav_graph"
        app:startDestination="@id/fragmentOne">

    <dialog android:id="@+id/fragmentOne"
        android:name="com.example.navcomponentapp.FragmentOne"
        android:label="fragment_fragment_one"
        tools:layout="@layout/fragment_fragment_one">

        <action android:id="@+id/action_fragmentOne_to_fragmentTwo2"
            app:destination="@id/fragmentTwo"/>
    </dialog>

    <dialog android:id="@+id/fragmentTwo"
        android:name="com.example.navcomponentapp.FragmentTwo"
        android:label="fragment_fragment_two"
        tools:layout="@layout/fragment_fragment_two"/>
</navigation>
Run Code Online (Sandbox Code Playgroud)

演示(这不是我想要的。我不想在另一个bottomSheet上打开一个bottomSheet

android android-navigation android-architecture-components android-jetpack android-navigation-graph

9
推荐指数
1
解决办法
1915
查看次数

底部导航视图 OnNavigationItemSelectedListener 已弃用

我正在按照Material Design尝试 Android 的 BottomNavigationView 实现

但是,在 MainActivity 代码上,我收到一条OnNavigationItemSelectedListener已弃用的警告- 请参阅下面的快照

在此处输入图片说明

尝试使用另一种方法来处理 BottomNavigationView,但我找不到它。

寻求任何有出路的人的帮助,但与此同时,我已将我的 BottomView 的菜单项 ID 与片段目标 ID 进行了匹配,并且我成功实现了导航,但存在无法使用片段名称更新我的工具栏标题的限制。

android android-navigation

9
推荐指数
3
解决办法
2587
查看次数

类“action”不是抽象的,也没有实现抽象成员 public abstract val actionId: Int 在 androidx.navigation.NavDirections 中定义

我使用的版本2.4.0-alpha04是为了方便multi-backstack使用navigation. (开发文档

但我收到以下错误:

Class 'ActionDialogToWriteRoutine' is not abstract and does not implement abstract member public abstract val actionId: Int defined in androidx.navigation.NavDirections
Run Code Online (Sandbox Code Playgroud)

我首先在这里搜索以修复错误,并发现具有相同错误的问题。(这里

我在该链接中看到一个答案,说该错误已从 开始修复v2.4.0-alpha02

但我仍然遇到同样的错误。

所以我根据我的情况再次发布这个问题。

发生了什么?

构建.gradle[应用程序]

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'

    // safe args
    id 'androidx.navigation.safeargs.kotlin'
}

dependencies {
    def lifecycle_version = "2.3.1"
    def nav_version = "2.4.0-alpha04"

    // AAC
    implementation("androidx.fragment:fragment-ktx:1.3.4")

    // Navigation Component
    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
    implementation "androidx.navigation:navigation-ui-ktx:$nav_version"

}
Run Code Online (Sandbox Code Playgroud)

nav_graph.xml

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" …
Run Code Online (Sandbox Code Playgroud)

android kotlin android-navigation

9
推荐指数
1
解决办法
4932
查看次数

Jetpack 撰写。导航非法状态异常

当我在目的地之间快速切换(即底部导航:主屏幕 -> 设置屏幕 -> 主屏幕)时出现此异常。导航是根据文档sampleApp中的示例实现的。也许这是 Jetpack Compose 的错误,因为我在互联网上没有找到类似的错误。

出现异常

屏幕:

sealed class Screen(
val route: String,
@StringRes val title: Int,
@DrawableRes val icon: Int) {

object Home : Screen("HomeScreen", R.string.app_name, R.drawable.ic_baseline_chevron_right_24)
object Scanner : Screen("ScannerScreen", R.string.app_name, R.drawable.ic_baseline_chevron_right_24)
object Settings : Screen("SettingsScreen", R.string.app_name, R.drawable.ic_baseline_chevron_right_24)
}
Run Code Online (Sandbox Code Playgroud)

底部导航:

@Composable
fun BottomNavigationBar(navController: NavController) {
    val items = listOf(
        Screen.Home,
        Screen.Scanner,
        Screen.Settings
    )
    BottomNavigation {
        val navBackStackEntry = navController.currentBackStackEntryAsState()
        val currentRoute = navBackStackEntry.value?.destination?.route
        items.forEach { item ->
            BottomNavigationItem(
                icon = { Icon(painter …
Run Code Online (Sandbox Code Playgroud)

android kotlin android-navigation android-jetpack-compose

9
推荐指数
1
解决办法
3097
查看次数

Android 离开项目选项卡时清除后退栈 BottomNavigation

我正在使用最后一个导航库(版本 2.4.0),其中包括多个带有底部导航视图的返回堆栈。

默认行为是当用户返回选项卡时保存每个返回堆栈。我想在用户离开选项卡时清除后退堆栈。

基于此带有 3 个选项卡的底部导航:

表1 表2 表3
屏幕A1 屏幕B1 屏幕C1
屏幕A2 屏幕B2 屏幕C2

从 Tab1 中,如果用户:

  • 转到屏幕A2
  • 单击 Tab2(显示 ScreenB1)
  • 返回选项卡1
  • 这里我想要 ScreenA1 而不是 ScreenA2 (导航库的默认行为)

谢谢

android android-navigation bottomnavigationview android-bottom-nav-view android-architecture-navigation

9
推荐指数
2
解决办法
1821
查看次数