【问题标题】:Not able to destroy fragment from Nav Controller in Android无法从 Android 中的导航控制器销毁片段
【发布时间】:2021-03-01 17:22:44
【问题描述】:

我的问题是一样的:Kill fragment in navigation controller

但我使用绑定,因为它正在获取 NPE,我没有要传递的 id

代码:点击提交按钮时,我这样做了,所以它来到 FragmentA,但是当我按下返回时,再次来到 C,而不是用户应该退出应用程序。

val action = CheckoutFragmentDirections.actionCheckoutFragmentToProductFragment()
        findNavController().navigate(action)

【问题讨论】:

    标签: android kotlin


    【解决方案1】:

    在 nav_graph 里面添加这个 action 标签

    app:popUpTo="@id/homeFragment"
    

    完整代码:

    fragment
            android:id="@+id/welcomeFragment"
            android:name="com.jimmytrivedi.kotlin.navcomponent.WelcomeFragment"
            android:label="{username}"
            tools:layout="@layout/fragment_welcome">
            <argument
                android:name="username"
                app:argType="string" />
            <argument
                android:name="password"
                app:argType="string" />
            <action
                android:id="@+id/action_welcomeFragment_to_homeFragment"
                app:destination="@id/homeFragment"
                app:enterAnim="@anim/slide_in_right"
                app:exitAnim="@anim/slide_out_left"
                app:popEnterAnim="@anim/slide_in_left"
                app:popExitAnim="@anim/slide_out_right"
                app:popUpTo="@id/homeFragment"
                app:popUpToInclusive="true" />
        </fragment>
    

    【讨论】:

      【解决方案2】:

      这个代码块对我有用

      navController.popBackStack(R.id.A, false)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-06-16
        • 1970-01-01
        • 2022-01-03
        • 1970-01-01
        • 1970-01-01
        • 2019-07-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多