使用导航组件从一个活动导航到另一个活动 [英] Navigate from one activity to another with Navigation Component
问题描述
我正在尝试从一个导航图(使用 LoginActivity
上的主机片段)导航到另一个导航图(使用 HomeActivity
上的主机片段).我知道 Google 提倡使用单一活动应用程序,因此我只会使用这两个活动(以保持最少).
I am trying to navigate from one navigation graph (with the host fragment on the LoginActivity
) to another navigation graph (with the host fragment on the HomeActivity
). I know Google is advocating for single activity applications and thus I am only going to be using these two Activities (to keep it minimal).
我的login_navigation.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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mobile_navigation"
app:startDestination="@id/nav_login">
<fragment
android:id="@+id/nav_login"
android:name="your.appname.LoginFragment"
android:label="@string/login_menu"
tools:layout="@layout/fragment_login" >
<action
android:id="@+id/action_nav_login_to_enterCellphone"
app:destination="@id/login_enter_cellphone"
app:popUpTo="@id/nav_login" />
</fragment>
<fragment
android:id="@+id/login_enter_cellphone"
android:name="your.appname.LoginEnterCellphoneFragment"
android:label="@string/enter_cellphone_fragment_label"
tools:layout="@layout/fragment_login_enter_cellphone" >
<action
android:id="@+id/action_enter_cellphone_to_enterOTPFragment"
app:destination="@id/enterOTPFragment"
app:popUpTo="@id/login_enter_cellphone" />
</fragment>
<fragment
android:id="@+id/enterOTPFragment"
android:name="your.appname.EnterOTPFragment"
android:label="@string/enter_otp_label"
tools:layout="@layout/fragment_enter_o_t_p" >
<action
android:id="@+id/action_enterOTPFragment_to_main_navigation"
app:destination="@id/main_navigation"
app:launchSingleTop="false" />
</fragment>
<include app:graph="@navigation/main_navigation" />
</navigation>
和main_navigation.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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_navigation"
app:startDestination="@id/nav_home">
<fragment
android:id="@+id/nav_home"
android:name="your.appname.HomeFragment"
android:label="Home"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/nav_profile"
android:name="your.appname.ProfileFragment"
android:label="@string/profile_menu"
tools:layout="@layout/fragment_profile" />
</navigation>
我找到了这个答案并按如下方式实现了
I found this answer and implemented it as follows
private fun navigateToHomeActivity() {
val action = EnterOTPFragmentDirections.actionEnterOTPFragmentToMainNavigation()
findNavController().navigate(action)
(activity as LoginActivity).finish() <-- later commented out
}
但它给了我以下错误 E/InputMethodManager: prepareNavigationBarInfo() rootView is null
如果我注释掉上面的行,错误就会消失,但它只会用 HomeFragment
替换当前片段.
If I comment out the above line the error goes away but it only replaces the current fragment with the HomeFragment
.
如何导航到另一个活动,以便删除源活动(无法导航回它)并使用目标活动?
How do I navigate to another Activity so that the source Activity is removed (Can't navigate back to it) and the Destination Activity is used?
推荐答案
您可以定义 2 个导航图:
You can define 2 navigation graphs:
ActivityA
带导航Graph A
ActivityB
带导航Graph B
ActivityA
with navigationGraph A
ActivityB
with navigationGraph B
在GraphA
中你可以定义一个目的地来启动ActivityB
In the GraphA
you can define a destination to start ActivityB
<!-- Graph A -->
<navigation
....
<activity
android:id="@+id/activityB"
android:name=".ActivityB"
...>
<argument
.... />
</activity>
</navigation>
然后您可以像使用目的地一样导航到该活动(= startActivity(intent)
):
Then you can navigate to that activity ( = startActivity(intent)
) using it like a destination:
Navigation.findNavController(view).navigate(R.id.activityB);
更多信息此处.
这篇关于使用导航组件从一个活动导航到另一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!