[ad_1]
you can use viewModel to share data between fragment.
class SharedViewModel() : ViewModel(){
val sharedString = "some string"
}
on fragment1:
class Fragment1 : Fragment() {
private val viewModel: SharedViewModel by activityViewModels()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Log.e(TAG, viewModel.sharedString) // some String
............
on fragment2:
class Fragment2 : Fragment() {
private val viewModel: SharedViewModel by activityViewModels()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Log.e(TAG, viewModel.sharedString) // some String
............
P.S : make sure you the viewModel have same instance on both fragment, you can learn more on documentation
[ad_2]