I have a Fragment A containing a Fragment B.
I want to pass an argument to Fragment B from Fragment A when Fragment A is in the onActivityCreated lifecycle (because I have a data coming from a viewmodel that arrives at this moment).
In my Fragment B I cannot get the argument. I have a null exception.
Do you have a solution to my problem?
Here is my code
Fragment 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
}
}
FragmentB
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>