无法将参数自 Fragment 传递给子 Fragment

Cannot pass arguments since Fragment to child Fragment

我有一个包含片段 B 的片段 A。

当 Fragment A 处于 onActivityCreated 生命周期时,我想从 Fragment A 向 Fragment B 传递一个参数(因为我有一个来自此时到达的视图模型的数据)。

在我的片段 B 中,我无法获取参数。我有一个空异常。

你有办法解决我的问题吗?

这是我的代码

片段A

class FragmentA: Fragment() {

    private lateinit var fragmentB: FragmentB

    /**
     * @inheritdocs
     */
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_a, container, false)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        this.fragmentB = FragmentB()
        val transaction = childFragmentManager.beginTransaction()
        transaction.add(R.id.fragmentB, fragmentB).commit()
    }

    /**
     * @inheritdocs
     */
    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        val args = Bundle()
        args.putString("name", "test")
        this.fragmentB.arguments = args
    }
}

片段B

class FragmentB: Fragment() {


    /**
     * @inheritdocs
     */
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_b, container, false)
    }

    /**
     * @inheritdocs
     */
    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        var name = this.arguments?.getString("name")!!
    }
}

fragment_a.xml

<androidx.constraintlayout.widget.ConstraintLayout 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/fragmentB"
        android:name="com.test.view.FragmentB"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

您已在 xml 布局中声明了 Fragment。这意味着它是在没有参数的情况下创建的。如果您从代码中添加 Fragment,则无需在 xml.

中声明它

所以只需将您的 xml 更改为:

<androidx.constraintlayout.widget.ConstraintLayout       
    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">

    <FrameLayout
        android:id="@+id/fragmentB"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

在你的片段 B 中实现静态方法来初始化片段,如下所示。

public static FragmentB newInstance(string x) {
  FragmentB f = new FragmentB ();

  Bundle args = new Bundle();
  args.putInt("name", x);
  f.setArguments(args);
  return f;
}

你可以在FragmentA中使用这个方法

here

中阅读更多内容

如果您的数据位于视图模型中,则片段本身没有理由不能直接访问它。 viewmodels 的优点之一是易于在其构成组件之间共享屏幕数据 views/fragments/whatevers.

可以参考文档here