简体   繁体   English

Android 导航组件后退按钮不起作用

[英]Android Navigation Component back button not working

I'm using the Navigation Component in android where I have set 6 fragments initially.我在 android 中使用导航组件,我最初设置了 6 个片段。 The problem is when I added a new fragment (ProfileFragment).问题是当我添加一个新片段(ProfileFragment)时。

When I navigate to this new fragment from the start destination, pressing the native back button does not pop the current fragment off.当我从起始目的地导航到这个新片段时,按下本机后退按钮不会弹出当前片段。 Instead, it just stays to the fragment I'm in - the back button does nothing.相反,它只停留在我所在的片段上——后退按钮什么都不做。

Here's my navigation.xml :这是我的导航。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>

在此处输入图像描述

In the image above, the highlighted arrow (in the left) is the navigation action I'm having troubles with.在上图中,突出显示的箭头(左侧)是我遇到问题的导航操作。

In my Fragment code, I'm navigating as follows:在我的片段代码中,我的导航如下:

findNavController().navigate(R.id.action_dashboardFragment_to_profileFragment)

The other navigation actions are working as intended.其他导航操作按预期工作。 But for some reason, this newly added fragment does not behave as intended.但由于某种原因,这个新添加的片段并没有按预期运行。

There are no logs showing when I navigate to ProfileFragment and when I press the back button.当我导航到 ProfileFragment 和按下后退按钮时,没有显示日志。

Am I missing something?我错过了什么吗? or is there anything wrong with my action/fragment configurations?还是我的动作/片段配置有什么问题?

EDIT: I do not do anything in ProfileFragment .编辑:我在ProfileFragment中没有做任何事情。 Here's the code for it:这是它的代码:

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)
    }


}

And my activity xml containing the nav host:我的活动 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>

I'm using the Navigation Component in android where I have set 6 fragments initially.我正在使用android中的导航组件,其中最初设置了6个片段。 The problem is when I added a new fragment (ProfileFragment).问题是当我添加一个新片段 (ProfileFragment) 时。

When I navigate to this new fragment from the start destination, pressing the native back button does not pop the current fragment off.当我从开始目的地导航到这个新片段时,按下本机后退按钮不会弹出当前片段。 Instead, it just stays to the fragment I'm in - the back button does nothing.相反,它只停留在我所在的片段中——后退按钮什么也不做。

Here's my navigation.xml :这是我的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>

在此处输入图片说明

In the image above, the highlighted arrow (in the left) is the navigation action I'm having troubles with.在上图中,突出显示的箭头(左侧)是我遇到问题的导航操作。

In my Fragment code, I'm navigating as follows:在我的Fragment代码中,我导航如下:

findNavController().navigate(R.id.action_dashboardFragment_to_profileFragment)

The other navigation actions are working as intended.其他导航操作按预期工作。 But for some reason, this newly added fragment does not behave as intended.但是由于某种原因,这个新添加的片段没有按预期运行。

There are no logs showing when I navigate to ProfileFragment and when I press the back button.当我导航到 ProfileFragment 和按下后退按钮时,没有显示日志。

Am I missing something?我错过了什么吗? or is there anything wrong with my action/fragment configurations?或者我的动作/片段配置有什么问题吗?

EDIT: I do not do anything in ProfileFragment .编辑:我在ProfileFragment中什么也不做。 Here's the code for it:这是它的代码:

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)
    }


}

And my activity xml containing the nav host:我的活动 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>

I'm using the Navigation Component in android where I have set 6 fragments initially.我正在使用android中的导航组件,其中最初设置了6个片段。 The problem is when I added a new fragment (ProfileFragment).问题是当我添加一个新片段 (ProfileFragment) 时。

When I navigate to this new fragment from the start destination, pressing the native back button does not pop the current fragment off.当我从开始目的地导航到这个新片段时,按下本机后退按钮不会弹出当前片段。 Instead, it just stays to the fragment I'm in - the back button does nothing.相反,它只停留在我所在的片段中——后退按钮什么也不做。

Here's my navigation.xml :这是我的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>

在此处输入图片说明

In the image above, the highlighted arrow (in the left) is the navigation action I'm having troubles with.在上图中,突出显示的箭头(左侧)是我遇到问题的导航操作。

In my Fragment code, I'm navigating as follows:在我的Fragment代码中,我导航如下:

findNavController().navigate(R.id.action_dashboardFragment_to_profileFragment)

The other navigation actions are working as intended.其他导航操作按预期工作。 But for some reason, this newly added fragment does not behave as intended.但是由于某种原因,这个新添加的片段没有按预期运行。

There are no logs showing when I navigate to ProfileFragment and when I press the back button.当我导航到 ProfileFragment 和按下后退按钮时,没有显示日志。

Am I missing something?我错过了什么吗? or is there anything wrong with my action/fragment configurations?或者我的动作/片段配置有什么问题吗?

EDIT: I do not do anything in ProfileFragment .编辑:我在ProfileFragment中什么也不做。 Here's the code for it:这是它的代码:

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)
    }


}

And my activity xml containing the nav host:我的活动 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>

I'm using the Navigation Component in android where I have set 6 fragments initially.我正在使用android中的导航组件,其中最初设置了6个片段。 The problem is when I added a new fragment (ProfileFragment).问题是当我添加一个新片段 (ProfileFragment) 时。

When I navigate to this new fragment from the start destination, pressing the native back button does not pop the current fragment off.当我从开始目的地导航到这个新片段时,按下本机后退按钮不会弹出当前片段。 Instead, it just stays to the fragment I'm in - the back button does nothing.相反,它只停留在我所在的片段中——后退按钮什么也不做。

Here's my navigation.xml :这是我的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>

在此处输入图片说明

In the image above, the highlighted arrow (in the left) is the navigation action I'm having troubles with.在上图中,突出显示的箭头(左侧)是我遇到问题的导航操作。

In my Fragment code, I'm navigating as follows:在我的Fragment代码中,我导航如下:

findNavController().navigate(R.id.action_dashboardFragment_to_profileFragment)

The other navigation actions are working as intended.其他导航操作按预期工作。 But for some reason, this newly added fragment does not behave as intended.但是由于某种原因,这个新添加的片段没有按预期运行。

There are no logs showing when I navigate to ProfileFragment and when I press the back button.当我导航到 ProfileFragment 和按下后退按钮时,没有显示日志。

Am I missing something?我错过了什么吗? or is there anything wrong with my action/fragment configurations?或者我的动作/片段配置有什么问题吗?

EDIT: I do not do anything in ProfileFragment .编辑:我在ProfileFragment中什么也不做。 Here's the code for it:这是它的代码:

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)
    }


}

And my activity xml containing the nav host:我的活动 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>

I'm using the Navigation Component in android where I have set 6 fragments initially.我正在使用android中的导航组件,其中最初设置了6个片段。 The problem is when I added a new fragment (ProfileFragment).问题是当我添加一个新片段 (ProfileFragment) 时。

When I navigate to this new fragment from the start destination, pressing the native back button does not pop the current fragment off.当我从开始目的地导航到这个新片段时,按下本机后退按钮不会弹出当前片段。 Instead, it just stays to the fragment I'm in - the back button does nothing.相反,它只停留在我所在的片段中——后退按钮什么也不做。

Here's my navigation.xml :这是我的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>

在此处输入图片说明

In the image above, the highlighted arrow (in the left) is the navigation action I'm having troubles with.在上图中,突出显示的箭头(左侧)是我遇到问题的导航操作。

In my Fragment code, I'm navigating as follows:在我的Fragment代码中,我导航如下:

findNavController().navigate(R.id.action_dashboardFragment_to_profileFragment)

The other navigation actions are working as intended.其他导航操作按预期工作。 But for some reason, this newly added fragment does not behave as intended.但是由于某种原因,这个新添加的片段没有按预期运行。

There are no logs showing when I navigate to ProfileFragment and when I press the back button.当我导航到 ProfileFragment 和按下后退按钮时,没有显示日志。

Am I missing something?我错过了什么吗? or is there anything wrong with my action/fragment configurations?或者我的动作/片段配置有什么问题吗?

EDIT: I do not do anything in ProfileFragment .编辑:我在ProfileFragment中什么也不做。 Here's the code for it:这是它的代码:

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)
    }


}

And my activity xml containing the nav host:我的活动 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>

I'm using the Navigation Component in android where I have set 6 fragments initially.我正在使用android中的导航组件,其中最初设置了6个片段。 The problem is when I added a new fragment (ProfileFragment).问题是当我添加一个新片段 (ProfileFragment) 时。

When I navigate to this new fragment from the start destination, pressing the native back button does not pop the current fragment off.当我从开始目的地导航到这个新片段时,按下本机后退按钮不会弹出当前片段。 Instead, it just stays to the fragment I'm in - the back button does nothing.相反,它只停留在我所在的片段中——后退按钮什么也不做。

Here's my navigation.xml :这是我的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>

在此处输入图片说明

In the image above, the highlighted arrow (in the left) is the navigation action I'm having troubles with.在上图中,突出显示的箭头(左侧)是我遇到问题的导航操作。

In my Fragment code, I'm navigating as follows:在我的Fragment代码中,我导航如下:

findNavController().navigate(R.id.action_dashboardFragment_to_profileFragment)

The other navigation actions are working as intended.其他导航操作按预期工作。 But for some reason, this newly added fragment does not behave as intended.但是由于某种原因,这个新添加的片段没有按预期运行。

There are no logs showing when I navigate to ProfileFragment and when I press the back button.当我导航到 ProfileFragment 和按下后退按钮时,没有显示日志。

Am I missing something?我错过了什么吗? or is there anything wrong with my action/fragment configurations?或者我的动作/片段配置有什么问题吗?

EDIT: I do not do anything in ProfileFragment .编辑:我在ProfileFragment中什么也不做。 Here's the code for it:这是它的代码:

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)
    }


}

And my activity xml containing the nav host:我的活动 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>

I'm using the Navigation Component in android where I have set 6 fragments initially.我正在使用android中的导航组件,其中最初设置了6个片段。 The problem is when I added a new fragment (ProfileFragment).问题是当我添加一个新片段 (ProfileFragment) 时。

When I navigate to this new fragment from the start destination, pressing the native back button does not pop the current fragment off.当我从开始目的地导航到这个新片段时,按下本机后退按钮不会弹出当前片段。 Instead, it just stays to the fragment I'm in - the back button does nothing.相反,它只停留在我所在的片段中——后退按钮什么也不做。

Here's my navigation.xml :这是我的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>

在此处输入图片说明

In the image above, the highlighted arrow (in the left) is the navigation action I'm having troubles with.在上图中,突出显示的箭头(左侧)是我遇到问题的导航操作。

In my Fragment code, I'm navigating as follows:在我的Fragment代码中,我导航如下:

findNavController().navigate(R.id.action_dashboardFragment_to_profileFragment)

The other navigation actions are working as intended.其他导航操作按预期工作。 But for some reason, this newly added fragment does not behave as intended.但是由于某种原因,这个新添加的片段没有按预期运行。

There are no logs showing when I navigate to ProfileFragment and when I press the back button.当我导航到 ProfileFragment 和按下后退按钮时,没有显示日志。

Am I missing something?我错过了什么吗? or is there anything wrong with my action/fragment configurations?或者我的动作/片段配置有什么问题吗?

EDIT: I do not do anything in ProfileFragment .编辑:我在ProfileFragment中什么也不做。 Here's the code for it:这是它的代码:

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)
    }


}

And my activity xml containing the nav host:我的活动 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>

I'm using the Navigation Component in android where I have set 6 fragments initially.我正在使用android中的导航组件,其中最初设置了6个片段。 The problem is when I added a new fragment (ProfileFragment).问题是当我添加一个新片段 (ProfileFragment) 时。

When I navigate to this new fragment from the start destination, pressing the native back button does not pop the current fragment off.当我从开始目的地导航到这个新片段时,按下本机后退按钮不会弹出当前片段。 Instead, it just stays to the fragment I'm in - the back button does nothing.相反,它只停留在我所在的片段中——后退按钮什么也不做。

Here's my navigation.xml :这是我的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>

在此处输入图片说明

In the image above, the highlighted arrow (in the left) is the navigation action I'm having troubles with.在上图中,突出显示的箭头(左侧)是我遇到问题的导航操作。

In my Fragment code, I'm navigating as follows:在我的Fragment代码中,我导航如下:

findNavController().navigate(R.id.action_dashboardFragment_to_profileFragment)

The other navigation actions are working as intended.其他导航操作按预期工作。 But for some reason, this newly added fragment does not behave as intended.但是由于某种原因,这个新添加的片段没有按预期运行。

There are no logs showing when I navigate to ProfileFragment and when I press the back button.当我导航到 ProfileFragment 和按下后退按钮时,没有显示日志。

Am I missing something?我错过了什么吗? or is there anything wrong with my action/fragment configurations?或者我的动作/片段配置有什么问题吗?

EDIT: I do not do anything in ProfileFragment .编辑:我在ProfileFragment中什么也不做。 Here's the code for it:这是它的代码:

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)
    }


}

And my activity xml containing the nav host:我的活动 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>

I'm using the Navigation Component in android where I have set 6 fragments initially.我正在使用android中的导航组件,其中最初设置了6个片段。 The problem is when I added a new fragment (ProfileFragment).问题是当我添加一个新片段 (ProfileFragment) 时。

When I navigate to this new fragment from the start destination, pressing the native back button does not pop the current fragment off.当我从开始目的地导航到这个新片段时,按下本机后退按钮不会弹出当前片段。 Instead, it just stays to the fragment I'm in - the back button does nothing.相反,它只停留在我所在的片段中——后退按钮什么也不做。

Here's my navigation.xml :这是我的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>

在此处输入图片说明

In the image above, the highlighted arrow (in the left) is the navigation action I'm having troubles with.在上图中,突出显示的箭头(左侧)是我遇到问题的导航操作。

In my Fragment code, I'm navigating as follows:在我的Fragment代码中,我导航如下:

findNavController().navigate(R.id.action_dashboardFragment_to_profileFragment)

The other navigation actions are working as intended.其他导航操作按预期工作。 But for some reason, this newly added fragment does not behave as intended.但是由于某种原因,这个新添加的片段没有按预期运行。

There are no logs showing when I navigate to ProfileFragment and when I press the back button.当我导航到 ProfileFragment 和按下后退按钮时,没有显示日志。

Am I missing something?我错过了什么吗? or is there anything wrong with my action/fragment configurations?或者我的动作/片段配置有什么问题吗?

EDIT: I do not do anything in ProfileFragment .编辑:我在ProfileFragment中什么也不做。 Here's the code for it:这是它的代码:

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)
    }


}

And my activity xml containing the nav host:我的活动 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>

The Simplest Answer for your problem (If it has something to do with fragments - Bottom navigation) could be您的问题的最简单答案(如果它与片段有关 - 底部导航)可能是

To set defaultNavHost = "false"设置 defaultNavHost = "false"

From Official Documentation it says-> Let's say you have 3 fragments set for Bottom Navigation, then setting从官方文档中说->假设您为底部导航设置了 3 个片段,然后设置

"defaultNavHost = true" will make fragment A acts like a parent, so when user clicks on back button in fragment 3, it comes to fragment 1 instead of closing the activity (Bottom Navigation as Example). “defaultNavHost = true” 将使片段 A 像父级一样,因此当用户单击片段 3 中的后退按钮时,它会到达片段 1 而不是关闭活动(以底部导航为例)。

Your XML should look like this, if you wanna just press back and close the activity from any fragment you are in.您的 XML 应该看起来像这样,如果您只想按下并关闭您所在的任何片段的活动。

   <fragment
android:id="@+id/fragmentContainerView"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottom_nav"
app:defaultNavHost="false"
app:navGraph="@navigation/visit_summary_navigation" /> 

I'm using the Navigation Component in android where I have set 6 fragments initially.我正在使用android中的导航组件,其中最初设置了6个片段。 The problem is when I added a new fragment (ProfileFragment).问题是当我添加一个新片段 (ProfileFragment) 时。

When I navigate to this new fragment from the start destination, pressing the native back button does not pop the current fragment off.当我从开始目的地导航到这个新片段时,按下本机后退按钮不会弹出当前片段。 Instead, it just stays to the fragment I'm in - the back button does nothing.相反,它只停留在我所在的片段中——后退按钮什么也不做。

Here's my navigation.xml :这是我的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>

在此处输入图片说明

In the image above, the highlighted arrow (in the left) is the navigation action I'm having troubles with.在上图中,突出显示的箭头(左侧)是我遇到问题的导航操作。

In my Fragment code, I'm navigating as follows:在我的Fragment代码中,我导航如下:

findNavController().navigate(R.id.action_dashboardFragment_to_profileFragment)

The other navigation actions are working as intended.其他导航操作按预期工作。 But for some reason, this newly added fragment does not behave as intended.但是由于某种原因,这个新添加的片段没有按预期运行。

There are no logs showing when I navigate to ProfileFragment and when I press the back button.当我导航到 ProfileFragment 和按下后退按钮时,没有显示日志。

Am I missing something?我错过了什么吗? or is there anything wrong with my action/fragment configurations?或者我的动作/片段配置有什么问题吗?

EDIT: I do not do anything in ProfileFragment .编辑:我在ProfileFragment中什么也不做。 Here's the code for it:这是它的代码:

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)
    }


}

And my activity xml containing the nav host:我的活动 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>

I had faced the same issue due to the below "run blocking" code block.由于下面的“运行阻塞”代码块,我遇到了同样的问题。 So don't use it if not necessary.因此,如果没有必要,请不要使用它。

enter image description here在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM