为什么Android Navigation Component 屏幕不返回上一个Fragment,而是调用了previos Fragment 的onViewCreated 中的一个方法?

ken*_*ken 9 android android-fragments android-navigation android-architecture-navigation android-navigation-graph

我有2个片段通话CreateRoomFragmentDisplayPhotoFragment,导航图是这个样子的:

<navigation>    
<fragment
    android:id="@+id/createRoomFragment"
    android:name="package.room.CreateRoomFragment"
    android:label="Create a room"
    tools:layout="@layout/fragment_create_room">
    <action
        android:id="@+id/action_createRoomFragment_to_roomFragment"
        app:destination="@id/roomFragment" />
    <action
        android:id="@+id/action_createRoomFragment_to_displayPhotoFragment"
        app:destination="@id/displayPhotoFragment" />
</fragment>

<fragment
    android:id="@+id/displayPhotoFragment"
    android:name="package.fragment.DisplayPhotoFragment"
    android:label="fragment_display_photo"
    tools:layout="@layout/fragment_display_photo" >
    <argument android:name="bitmap"
              app:argType="android.graphics.Bitmap"/>
</fragment>
Run Code Online (Sandbox Code Playgroud)

所以当我想从 to 移动CreateRoomFragmentDisplayPhotoFragment,我使用如下 do:

NavDirections action = CreateRoomFragmentDirections.actionCreateRoomFragmentToDisplayPhotoFragment(selectedPhoto);
Navigation.findNavController(view).navigate(action);
Run Code Online (Sandbox Code Playgroud)

这样做,我可以导航到DisplayPhotoFragment.

但是当我按下back设备的按钮以及Back arrow工具栏中的 时,它无法返回到CreateRoomFragment

我试过这个,但仍然无法回到上一个片段:

requireActivity().getOnBackPressedDispatcher().addCallback(getViewLifecycleOwner(),
                new OnBackPressedCallback(true) {
                    @Override
                    public void handleOnBackPressed() {

                       navController.navigateUp();  //I tried this 
                       navController.popBackStack(R.id.createRoomFragment,false); //and also this
                    }
                });
Run Code Online (Sandbox Code Playgroud)

现在主要问题:

使用上面的代码,屏幕没有回到之前的Fragment( CreateRoomFragment)。它仍然卡在了DisplayPhotoFragment,但同时,CreateRoomFragment onViewCreated正在调用section中的一个API方法。

这是什么原因造成的?我该如何解决这个问题?

Myk*_*Myk 15

我有同样的问题。对我来说,问题是我使用 LiveData 布尔值来决定何时转到下一个片段。当我向后/向上导航时,布尔值仍然为真,因此它会再次自动向前导航。

  • 说真的......这对我来说是同样的情况,尝试了一切,但找不到发生了什么。非常感谢 (2认同)

San*_*ani 14

Android 维护一个返回堆栈,其中包含您访问过的目的地。当用户打开应用程序时,应用程序的第一个目的地放置在堆栈中。每次调用该navigate()方法都会将另一个目标放在堆栈顶部。Tapping Up 或 Back 分别调用NavController.navigateUp()NavController.popBackStack()方法,以从堆栈中删除(或弹出)顶部目标。

NavController.popBackStack()返回一个布尔值,指示它是否成功弹出回另一个目的地。返回 false 的最常见情况是您手动弹出图形的起始目的地。

当方法返回 false 时,NavController.getCurrentDestination()返回 null。您负责导航到新目的地或通过调用finish()您的活动来处理弹出窗口。

使用操作进行导航时,您可以选择使用操作的popUpTopopUpToInclusive参数从返回堆栈中弹出其他目的地。

class MyFragment : Fragment() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val onBackPressedCallback = object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                if (true == conditionForCustomAction) {
                    CustomActionHere()
                } else  NavHostFragment.findNavController(this@MyFragment).navigateUp();    
        }
    }
    requireActivity().onBackPressedDispatcher.addCallback(
        this, onBackPressedCallback
    )
    ...
}
Run Code Online (Sandbox Code Playgroud)