In summary, I have 2 fragments. one is ViewPlacesFragment and the other is MenuFragment. I have a MainActivity class too. inside ViewPlacesFragment there is RecyclerViewAdapter . Inside a RecyclerViewAdapter class, I implemented a callback for when Recyclerview Item is clicked, then it sends the item Id that is clicked to MainActivity. then Main Activity calls MenuFragment for showing it and pass that Id to this fragment. Everything is worked fine except the Id value not received in MenuFragment. I could print the correct Id value in Log.d("debug4", Id) and I checked the code but nothing is wrong. so why is the Id value received null in MenuFragment?
RecyclerAdapterAllPlaces:
Class RecyclerAdapterAllPlaces(var myReceivedData: PlaceModel?=null, val context:Context,val cellClickCallback: (id:String) -> Unit):RecyclerView.Adapter<RecyclerAdapterAllPlaces.Viewholder>(){
...
override fun onBindViewHolder(holder: Viewholder, position: Int) {
holder.itemView.setOnClickListener {cellClickCallback(myReceivedData!![position]._id)}
}
}
MainActivity:
Class MainActivity(){
//new callback method in kotlin
val callbackFrominsideFragment=RecyclerAdapterAllPlaces(null,this,::callbackInsideFragment)
private fun callbackInsideFragment(Id:String) {
Log.d("debug4",Id)
val fragmentTransaction = getSupportFragmentManager().beginTransaction()
val args = Bundle()
val destinationFragment= MenuFragment()
args.putString("Place_Id", Id)
destinationFragment.arguments = args
fragmentTransaction.replace(R.id.frame_container, MenuFragment())
fragmentTransaction.commit()
}
}
MenuFragment:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val bundle = Bundle()
val data = bundle.getString("Place_Id")
Log.d("debug", "it is $data")
if (data != null)
{
P_Id = data
Log.d("debug",P_Id)
}
}
Edit:
I edited the code and remove new bundle() but return null.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val bundle = this.arguments
if (bundle != null)
{
val data = bundle.getString("Place_Id")
P_Id = data!!
Log.d("debug",P_Id)
}}