Я знаю, что ответ уже есть, но это может кому-то помочь.Code snippet
В build.gradle добавить эту зависимость
ext{
...
navigation_version = '1.0.0-alpha11'
}
dependencies {
...
classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:$navigation_version"
}
В app / build.gradle
apply plugin: 'androidx.navigation.safeargs'
...
В графике навигации
<fragment
android:id="@+id/source_fragment_id"
android:name="app.test.SourceFragment"
android:label="@string/source_fragment_label"
tools:layout="@layout/source_fragment_layout">
<action
android:id="@+id/action_source_fragment_to_destination_fragment"
app:destination="@id/destination_fragment_id"
...
/>
</fragment>
<fragment
android:id="@+id/destination_fragment_id"
android:name="app.test.DestinationFragment"
android:label="@string/destination_fragment_label"
tools:layout="@layout/destination_fragment_layout">
<argument
android:name="variableName"
app:argType="app.test.data.model.CustomModel" />
...
</fragment>
Примечание: CustomModel должен быть Parcelable или Serializable.
При переходе к этому DestinationFragment из SourceFragment
val direction = SourceFragmentDirections.ActionSourceFragmentToDestinationFragment(customModel)
findNavController().navigate(direction)
Теперь получение значения из пакета в DestinationFragment
...
import app.test.DestinationFragmentArgs.fromBundle
class DestinationFragment : Fragment() {
val variableName by lazy {
fromBundle(arguments!!).variableName
}
...
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
Log.e(DESTINATION_FRAGMENT_TAG,"onCreateView")
//Can use CustomModel variable here i.e. variableName
}
}