【问题标题】:How to go to same Fragment from different Fragments using Navigation in android?如何在android中使用导航从不同的片段转到相同的片段?
【发布时间】:2021-05-07 09:16:25
【问题描述】:

我在同一个 Activity 中有 3 个不同的片段,并且 Activity 包含一个带有设置按钮的工具栏。当我按下设置按钮时,设置片段应该打开。如何使用导航来实现这一点?我应该从每个片段创建一个动作到设置片段还是有一种简单的方法? (考虑到设置按钮在活动布局中)。 我还创建了一个图表以便更好地理解。如果您有任何问题,请告诉我。谢谢

这是我在我的活动中设置导航图的方式:

private fun setDefaultNavGraph(bundle: Bundle?){
    fragmentContainer = my_nav_host_fragment as NavHostFragment
    val inflater = fragmentContainer.findNavController().navInflater
    val graph = inflater.inflate(R.navigation.prb_navigation)
    fragmentContainer.findNavController().setGraph(graph, bundle)
}

这是我的活动 xml 文件:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.eschbachit.citylinemobile.activities.PrbModuleActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/toolbar_height"
        android:background="@color/toolbarDarkGrey"
        app:theme="@style/ToolbarColoredWhiteIcon"
        app:contentInsetLeft="0dp"
        app:contentInsetStart="0dp"
        app:contentInsetRight="0dp"
        app:contentInsetEnd="0dp">

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/back_btn"
            android:layout_width="24dp"
            android:layout_height="match_parent"
            android:paddingTop="16dp"
            android:paddingBottom="16dp"
            android:layout_marginStart="4dp"
            app:srcCompat="@drawable/back_button_icon"
            android:tint="@color/white"
            android:visibility="gone"
            android:contentDescription="@null"/>

    <TextView
        android:id="@+id/prb_activity_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:ellipsize="end"
        android:text="@string/title_activity_main"
        android:textSize="@dimen/font_size_default"
        android:lineSpacingExtra="4sp"
        android:textStyle="bold"
        android:textColor="@color/white"
        android:layout_gravity="center"/>

    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/settings_nav_tv"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:textColor="@color/white"
        app:srcCompat="@drawable/ic_settings"
        android:layout_gravity="end"
        android:textSize="@dimen/font_size_default"
        android:paddingTop="12dp"
        android:paddingBottom="12dp"
        android:gravity="center" />

    </androidx.appcompat.widget.Toolbar>

    <fragment
        android:id="@+id/my_nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        android:layout_below="@+id/toolbar"/>

</RelativeLayout>

【问题讨论】:

    标签: android kotlin android-fragments navigation


    【解决方案1】:

    期望你的所有片段都位于一个活动上,你可以通过以下方式做到这一点:-

    1. 添加操作(如您所说)

    Navigation.findNavController(...).navigate(destination.id);

    1. 如果 Fragment A,B,C 的 parent 是 Settings Fragment,也可以 popBackStack。

      Navigation.findNavController(...).popBackStack();

    【讨论】:

    • 我必须实现 Navigation.findNavController(...).navigate(destination.id);在所有片段中?
    • 你能给我一个代码示例吗?请。
    【解决方案2】:

    解决方案:

    我正在寻找的东西是我的 nav_graph 中的通用操作。 我找到了解决方案here

    我将此操作添加到导航文件中,并在 setOnClickListener 方法的活动中使用它。

    <action android:id="@+id/openOnlineSettingsFragment"
        app:destination="@id/onlineSettingsFragment"
        app:enterAnim="@anim/pull_in_right"
        app:exitAnim="@anim/push_out_left"
        app:popEnterAnim="@anim/pull_in_left"
        app:popExitAnim="@anim/push_out_right" />
    

    【讨论】:

      猜你喜欢
      • 2021-11-24
      • 2020-03-21
      • 2016-10-24
      • 2020-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-20
      • 1970-01-01
      相关资源
      最近更新 更多