尝试将数据从活动发送到片段

ibps3vxo  于 2022-09-21  发布在  Android
关注(0)|答案(1)|浏览(166)

在……里面

override fun pastDada(name: String) {
        val bundle = Bundle()
        bundle.putString("USER_NAME", "DDDDDDDD")

        val transction = this.supportFragmentManager.beginTransaction()
        val profileFragmant = fragment_profile()
        profileFragmant.arguments = bundle
        transction.replace(R.id.container,profileFragmant)
    }

在Frafment:

override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        var view =  inflater.inflate(R.layout.fragment_profile2, container, false)

        name = arguments?.getString("USER_NAME")
        Log.e("name", arguments?.getString("USER_NAME").toString())

        Log.e("name", name.toString())
        view.tv_name.text = name

        return view
    }

E(“name”,name.toString())的结果总是空的,我不知道为什么maby因为我的参数是空的?如果是的话,我该如何去解决它呢?

谢谢

8hhllhi2

8hhllhi21#

如下所示更改代码

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment
    var view =  inflater.inflate(R.layout.fragment_profile2, container, false)

    val bundle = arguments
    val name = bundle!!.getString("USER_NAME")

    Log.e("name", name.toString())
    view.tv_name.text = name

    return view
}

相关问题