【问题标题】:Cannot pass arguments since Fragment to child Fragment无法将自 Fragment 以来的参数传递给子 Fragment
【发布时间】:2019-11-19 09:47:48
【问题描述】:

我有一个包含一个片段 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>

【问题讨论】:

    标签: android android-fragments kotlin fragment


    【解决方案1】:

    您已经在 xml 布局中声明了 Fragment。这意味着它是在没有 args 的情况下创建的。如果您是从代码中添加 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>
    

    【讨论】:

    • 那么我应该在 FragmentA 类的 onCreateView 中实例化 fragmentB 吗?
    • @vilpe89 是正确的。是的,使用 args 动态添加它。它会起作用的。
    • 谢谢,我忘了用FrameLayout更改片段属性
    【解决方案2】:

    在您的片段 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

    【讨论】:

      【解决方案3】:

      如果您在视图模型中有数据,则片段本身没有理由无法直接访问它。视图模型的优点之一是易于在其组成组件之间共享屏幕数据,无论它们是视图/片段/其他。

      您可以参考文档here。

      【讨论】:

        猜你喜欢
        • 2012-10-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-18
        • 2012-10-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多