title: Which is the better way to share data between Fragments?
body: <p>I want to share a few Integer values from one fragment to the other. I don't want lose the data when the device changes configuration.</p>
  <p>So the two ways that I came across and want to know which one will be better for my use case are:</p>
  <h3>1. Sharing a <code>ViewModel</code> among multiple fragments</h3>
  <pre><code>class SharedViewModel : ViewModel(){
      ...
  }
  
  class FragmentA : Fragment(){
      private val model: SharedViewModel by activityViewModels()
      ...
  }
  
  class FragmentB : Fragment(){
      private val model: SharedViewModel by activityViewModels()
      ...
  }
  </code></pre>
  <h3>2. Using combination of SafeArgs and custom <code>ViewModelProvider.Factory</code></h3>
  <p>Using SafeArgs to pass data as a parameter to a navigation action from a fragment (say A) to another fragmet (say B). Implementing <code>ViewModel</code> (parameterized) and <code>ViewModelFactory</code> classes for fragment B. Passing the data from SafeArgs to ViewModelFactory to create a ViewModel (using <code>ViewModelProvider</code>)</p>
  <p>Something like this:</p>
  <pre><code>class B : Fragment() {
  
      //Seperate classes for ViewModelB &amp; ViewModelFactoryB
      private lateinit var viewModel: ViewModelB
      private lateinit var viewModelFactory: ViewModelFactoryB
  
      override fun onCreateView(
              inflater: LayoutInflater,
              container: ViewGroup?,
              savedInstanceState: Bundle?
      ): View? {
  
          val binding: BFragmentBinding = DataBindingUtil.inflate(
                  inflater,
                  R.layout.b_fragment,
                  container,
                  false
          )
  
          viewModelFactory = ViewModelFactoryB(BFragmentArgs.fromBundle(requireArguments()).data)
          viewModel = ViewModelProvider(this, viewModelFactoryB).get(ViewModelB::class.java)
  
          return binding.root
      }
  }
  </code></pre>