【发布时间】:2020-05-07 02:50:38
【问题描述】:
我在 android 中使用导航组件,我最初设置了 6 个片段。问题是当我添加一个新片段(ProfileFragment)时。
当我从起始目的地导航到这个新片段时,按下本机后退按钮不会弹出当前片段。相反,它只是停留在我所在的片段中 - 后退按钮什么都不做。
这是我的 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/dashboard_navigation"
app:startDestination="@id/dashboardFragment"
>
<fragment
android:id="@+id/dashboardFragment"
android:name="com.devssocial.localodge.ui.dashboard.ui.DashboardFragment"
android:label="DashboardFragment"
>
<action
android:id="@+id/action_dashboardFragment_to_newPostFragment"
app:destination="@id/newPostFragment"
app:enterAnim="@anim/slide_in_up"
app:exitAnim="@anim/slide_out_down"
app:popEnterAnim="@anim/slide_in_up"
app:popExitAnim="@anim/slide_out_down"
/>
<action
android:id="@+id/action_dashboardFragment_to_notificationsFragment"
app:destination="@id/notificationsFragment"
app:enterAnim="@anim/slide_in_up"
app:exitAnim="@anim/slide_out_down"
app:popEnterAnim="@anim/slide_in_up"
app:popExitAnim="@anim/slide_out_down"
/>
<action
android:id="@+id/action_dashboardFragment_to_mediaViewer"
app:destination="@id/mediaViewer"
app:enterAnim="@anim/slide_in_up"
app:exitAnim="@anim/slide_out_down"
app:popEnterAnim="@anim/slide_in_up"
app:popExitAnim="@anim/slide_out_down"
/>
<action
android:id="@+id/action_dashboardFragment_to_postDetailFragment"
app:destination="@id/postDetailFragment"
app:enterAnim="@anim/slide_in_up"
app:exitAnim="@anim/slide_out_down"
app:popEnterAnim="@anim/slide_in_up"
app:popExitAnim="@anim/slide_out_down"
/>
====================== HERE'S THE PROFILE ACTION ====================
<action
android:id="@+id/action_dashboardFragment_to_profileFragment"
app:destination="@id/profileFragment"
app:enterAnim="@anim/slide_in_up"
app:exitAnim="@anim/slide_out_down"
app:popEnterAnim="@anim/slide_in_up"
app:popExitAnim="@anim/slide_out_down"
/>
=====================================================================
</fragment>
<fragment
android:id="@+id/profileFragment"
android:name="com.devssocial.localodge.ui.profile.ui.ProfileFragment"
android:label="fragment_profile"
tools:layout="@layout/fragment_profile"
/>
</navigation>
在上图中,突出显示的箭头(左侧)是我遇到问题的导航操作。
在我的 Fragment 代码中,我的导航如下:
findNavController().navigate(R.id.action_dashboardFragment_to_profileFragment)
其他导航操作按预期工作。但是由于某种原因,这个新添加的片段并没有按预期运行。
当我导航到 ProfileFragment 和按下后退按钮时,没有显示任何日志。
我错过了什么吗?还是我的动作/片段配置有什么问题?
编辑: 我没有在 ProfileFragment 中做任何事情。这是它的代码:
class ProfileFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_profile, container, false)
}
}
还有我的 activity xml 包含导航主机:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/dashboard_navigation"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:navGraph="@navigation/dashboard_navigation"
app:defaultNavHost="true"/>
</FrameLayout>
【问题讨论】:
-
this.findNavController().popBackStack()会为你工作 -
您的
ProfileFragment是否实现了任何custom back navigation?您在那里执行的任何自定义后退代码都将优先于 NavController。你能包含那个片段的代码吗? -
@ianhanniballake 我没有在 ProfileFragment 中做任何事情。我已经编辑了我的问题以包含它
-
@RakeshKumar 对不起,我不完全理解你的意思。我在哪里添加
this.findNavController().popBackStack()?并且我的仪表板操作已经有目的地作为 profileFragment。 -
您能否包括将
NavHostFragment添加到您的活动的位置(我假设通过您的布局XML 中的<fragment>标记)?后退按钮是否适用于您拥有的所有其他目的地?
标签: android android-fragments android-navigation