【问题标题】:How to navigate between two activities using jetpack navigation library如何使用 jetpack 导航库在两个活动之间导航
【发布时间】:2019-09-30 19:52:15
【问题描述】:

我正在开发一个简单的应用程序来测试 Android 中的 JetPack Navigation。我的应用程序有两个活动“Activity1”和“Activity2”。我知道 Jetpack 导航仅适用于 SingleActivity 应用程序,但我想看看是否有任何解决方案可以解决我的问题。 应用程序以“Activity1”开头。下面是导航xml

<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"
    android:id="@+id/mobile_nav"
    app:startDestination="@+id/fragmentA1">

<fragment
        android:id="@+id/fragmentA1"
        android:name="com.example.android.codelabs.navigation.FragmentA1"
        android:label="@string/home"
        tools:layout="@layout/fragment_a_one">

    <action
            android:id="@+id/go_to_fragmentA2"
            app:destination="@+id/fragmentA2"/>

</fragment>


<fragment
        android:id="@+id/fragmentA2"
        android:name="com.example.android.codelabs.navigation.FragmentA2"
        tools:layout="@layout/fragment_a_two">
    <argument
            android:name="flowStepNumber"
            app:argType="integer"
            android:defaultValue="1"/>

    <argument
            android:name="displayName"
            app:argType="string"
            android:defaultValue="Hello"/>


    <action
            android:id="@+id/go_to_fragmentA3"
            app:destination="@+id/fragmentA3"/>

</fragment>


<fragment android:id="@+id/fragmentA3"
          android:name="com.example.android.codelabs.navigation.FragmentA3"
          android:label="fragment_third"
          tools:layout="@layout/fragment_a_three">

    <action android:id="@+id/go_to_activityB"
            app:destination="@id/activityB"/>


</fragment>

<activity android:id="@+id/activityB"
          android:name="com.example.android.codelabs.navigation.registration.ActivityB"
          android:label="activity_registration_main"
          tools:layout="@layout/activity_registration_main">

    <argument android:name="firstName"
              app:argType="string"/>

</activity>
<fragment android:id="@+id/fragmentB1"
          android:name="com.example.android.codelabs.navigation.registration.FragmentB1"
          android:label="fragment_step1"
          tools:layout="@layout/fragment_b_one">

    <argument android:name="firstName"
              app:argType="string"/>

</fragment>

<fragment android:id="@+id/fragmentB2"
          android:name="com.example.android.codelabs.navigation.registration.FragmentB2"
          android:label="fragment_step2"
          tools:layout="@layout/fragment_b_two">

    <argument android:name="lastName"
              app:argType="string"/>

</fragment>

下面是来自ActivityB的代码

val navController = findNavController(R.id.registration_navHost)
navController.setGraph(R.navigation.mobile_navigation)
navController.navigate(R.id.fragmentB1,safeArgs.toBundle())

在 ActivityB 中获取 safeArgs 的代码

private val safeArgs by navArgs<ActivityBArgs>()

这是我的问题。 1. 如何使用 jetpack Navigation 从 ActivityB(FragmentB1) 导航到 ActivityA(FragmentA2)。 2. 有没有办法通过android中的Manager Classes访问backStack?

为什么我需要两个活动? 作为 ActivityA 一部分的 FragmentA1、FragmentA2 和 FragmentA3 不应该有任何底部导航和抽屉布局。根据在这些片段中选择的答案,ActivityB 中会显示底部导航和抽屉布局。在 ActivityB 上的任何位置按下后退按钮时,ActivityA 应该与 FragmentA3 一起活跃起来。 ActivityA 和 ActivityB 的工具栏也不同

【问题讨论】:

  • nav library 的全部意义在于有一个活动。
  • 但是导航库也支持ActivityDestinations。

标签: android-architecture-components android-jetpack android-navigation fragment-backstack android-jetpack-navigation


【解决方案1】:

尽管有两个活动,但我再次推荐单活动应用程序。我进行了一些测试来分离 Activity,因为一个有 BottomNavigation,一个没有 BottomNavigation(就像你的问题在这里)。但我总是坚持在活动之间导航,并且后台堆栈总是重置(据我测试)。

其他解决方案是嵌套导航图,但我避免使用一些嵌套,因为我认为它会混合导航。所以是的,我尝试找到另一种方法,这就是我现在测试和使用的方法。

一些解决方法

让我们创建一个新场景。我有 3 个页面有底部导航(仪表板、搜索、配置文件)。然后我有一个没有底部导航的页面,例如。从 Profile 打开的 ProfileDetail。所以在这种情况下,我将创建:

  1. NavigationHostActivity(具有底部导航的活动)
  2. DashboardFragment(片段)
  3. SearchFragment(片段)
  4. ProfileFragment(片段)
  5. ProfileDetailFragment(片段)

我将所有布局更改为片段,当它导航到任何布局时,它会检查它们是否需要底部导航。基本概念是Show / HideBottomNavigation。这是我使用的一些示例:

NavigationHostActivity

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

        this.bind(
            this.findNavController(R.id.navigation_host_fragment),
            this.binding.navigationBottomView
        )
    }

    fun bind(navigationController: NavController, bottomNavigationView: BottomNavigationView) {
        navigationController.addOnDestinationChangedListener { _, destination, _ ->
            this.hideBottomNavigation(bottomNavigationView)

            bottomNavigationView.menu.forEach {
                if (it.itemId == destination.id) {
                    this.showBottomNavigation(bottomNavigationView)
                }
            }
        }
    }


    fun hideBottomNavigation(bottomNavigationView: BottomNavigationView) { bottomNavigationView.visibility = View.GONE }
    fun showBottomNavigation(bottomNavigationView: BottomNavigationView) { bottomNavigationView.visibility = View.VISIBLE }

这将检查 Navigation 的目的地是否与您的 BottomNavigation 的菜单相同。如果它是底部导航菜单之一,则显示底部导航。

其实我把bindshowhide函数放在NavigationService里面,所以大致就是这么干的。这是为了更简单的例子。随意调整它。我也在这里使用 DataBinding 仅供参考。

navigation_host_fragment

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/navigation_bottom_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FFF"
            app:itemIconSize="22dp"
            app:itemIconTint="@color/state_bottom_navigation_view"
            app:itemTextColor="@color/state_bottom_navigation_view"
            app:labelVisibilityMode="labeled"
            app:layout_constraintBottom_toBottomOf="parent"
            app:menu="@menu/menu_dashboard" />

        <fragment
            android:id="@+id/navigation_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:defaultNavHost="true"
            app:layout_constraintBottom_toTopOf="@id/navigation_bottom_view"
            app:layout_constraintTop_toTopOf="parent"
            app:navGraph="@navigation/navigation_graph" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <data>

        <variable
            name="activity"
            type="*.view.NavigationHostActivity" />
    </data>

</layout>

menu_dashboard

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/dashboardFragment"
        android:title="Dashboard"/>

    <item
        android:id="@+id/searchFragment"
        android:title="Search" />

    <item
        android:id="@+id/profileFragment"
        android:title="Profile" />

</menu>

就是这样,非常适合我的情况。希望对你有帮助:D

问候,

安德烈亚

【讨论】:

  • 你看过google AdvanceNavigation的例子吗?我想这样做,但使用导航扩展。这是repo的链接
【解决方案2】:

Activity 比较麻烦,但是你可以通过使用子 Fragment 在单 Activity 应用程序中实现相同的效果。

  1. 创建两个片段,FragmentAFragmentB。将所有 UI 从 ActivityA 移动到 FragmentA 并从 ActivityB 移动到 FragmentBActivityA 应该只持有NavHostFragment。删除ActivityB
  2. 创建包含FragmentAFragmentB 的主导航图并连接它们。将图表安装到ActivityA
  3. 创建另一个导航图,包含FragmentA1FragmentA2FragmentA3,并将其安装到NavHostFragment 中声明的FragmentA
  4. 使用FragmentB1FragmentB2 再创建一个导航图,并将其安装到FragmentB 中声明的NavHostFragment

就是这样。现在,当您需要在 FragmentAFragmentB 之间导航或返回时,只需使用父片段中的导航控制器:parentFragment?.findNavController() 或来自父片段的视图模型(如果您使用视图模型进行导航)。

【讨论】:

  • 您能否更清楚一点,或者向我展示一个示例代码,说明图形的外观以及我如何仅使用片段(没有任何活动)来实现底部导航和导航抽屉,因为您建议删除 ActivityB
  • 有什么解决办法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-10
  • 1970-01-01
  • 2016-07-10
  • 1970-01-01
相关资源
最近更新 更多