为什么bundle.getString()对于另一个片段接收到的数据为null



总之,我有两个片段。一个是ViewPlacesFragment,另一个是MenuFragment。我还有一节MainActivity课。ViewPlacesFragment内部有RecyclerViewAdapter。在RecyclerViewAdapter类中,我实现了一个回调,用于当单击Recyclerview Item时,它将单击的项目Id发送到MainActivity。则Main Activity调用MenuFragment来显示它,并将该Id传递给该片段。除了MenuFragment中未接收到Id值外,所有操作都很好。我可以在Log.d中打印正确的Id值("debug4",Id(,我检查了代码,但没有错。那么,为什么MenuFragment中接收到的Id值为null呢?

回收适配器所有位置:

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)}
}
}

主要活动:

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()
}

}

菜单片段:

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)
}

}

编辑:

我编辑了代码并删除了new bundle((,但返回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)
}}

在callbackInsideFragment方法中,您正在创建一个目标片段,但在fragmentTransaction.replace中,您传递了一个新的片段实例。

您正在创建一个新的捆绑包,而不是使用提供的捆绑包

//val bundle = Bundle() remove this line, that is instantiating a new bundle
val data = bundle?.getString("Place_Id")

你可以照你现在的样子做,也可以

val data = arguments?.getString("Place_Id")

变量arguments是获取额外bundle的Kotlin方法。是可以为null的,因此必须使用可为null的运算符?。实际上,你可以在任何方法上使用arguments,例如,如果你在onViewCreatedonCreateView上做一些事情,那么如果不需要onCreate,你就不需要覆盖它来获得捆绑包。

@Mahdi M的答案是正确的,但我想我会详细说明一下。让我们看看你发布的代码,为了清晰起见,稍微重新排列了一下:

private fun callbackInsideFragment(Id:String) {
val args = Bundle()
args.putString("Place_Id", Id)
val destinationFragment= MenuFragment()
destinationFragment.arguments = args
val fragmentTransaction = getSupportFragmentManager().beginTransaction()
fragmentTransaction.replace(R.id.frame_container, MenuFragment())
fragmentTransaction.commit()
}

在事务中,您将创建MenuFragment的第二个实例,并替换为,而不是正确设置了参数的destinationFragment。这意味着argsdestinationFragment从来没有真正使用过,所以你基本上只有这个:

private fun callbackInsideFragment(Id:String) {
val fragmentTransaction = getSupportFragmentManager().beginTransaction()
fragmentTransaction.replace(R.id.frame_container, MenuFragment())
fragmentTransaction.commit()
}

只需将replace()调用更改为使用destinationFragment,一切都会好起来:

private fun callbackInsideFragment(Id:String) {
val args = Bundle()
args.putString("Place_Id", Id)
val destinationFragment= MenuFragment()
destinationFragment.arguments = args
val fragmentTransaction = getSupportFragmentManager().beginTransaction()
fragmentTransaction.replace(R.id.frame_container, destinationFragment)
fragmentTransaction.commit()
}

最新更新