I have a viewPager inside my application which contains 3 Fragments. First Fragment has a recyclerview on click of the item of recyclerview I pass some data to another fragment which is not the part of viewpager fragements. I am using the latest viewmodel logic to achieve this functionality and as per the steps mentioned by many websites I am doing the following this.
Creating the separate class which extends viewmodel
class MovieObservable : ViewModel() { private val selectedMovieID = MutableLiveData<Int>() fun setSelectedMovieID(id : Int){ selectedMovieID.value = id } fun getSelectedMovieID(): MutableLiveData<Int> { return selectedMovieID }Initialize the viewmodel inside oncreateView Fragment
private lateinit var mViewModel : MovieObservable mViewModel = ViewModelProvider(this).get(MovieObservable::class.java)Now from my RecyclerView I have implemented the click listener and implemented that in my fragment and override the onclick event with the position and value
override fun onClickNowPlaying(position: Int, view: View, result: Result) { System.out.println("now playing ID" + result.id) mViewModel.setSelectedMovieID(result.id) val newFragment = MovieDetailsFragment() val transaction = activity?.supportFragmentManager?.beginTransaction() transaction?.replace(R.id.mainLayout, newFragment, "NewFragment") transaction?.addToBackStack(null) transaction?.commit() }I add the logic of observing the value in the details fragment which is below
private lateinit var mViewModel : MovieObservable mViewModel = ViewModelProvider(this).get(MovieObservable::class.java) mViewModel.getSelectedMovieID().observe(viewLifecycleOwner, Observer {getmydata-> System.out.println("in the next fragment " + getmydata) })
So All logic is finished to pass the data from one fragment to another but the problem is I am not getting the data on the details fragment. What is it which I am missing here.